我如何解释这个GET请求?



我有一个Angular应用程序,它使用一个服务发送GET请求来从MongoDB数据库检索数据。其中一个方法搜索名为Estimate的自定义对象,该对象具有以下属性:

Estimate.model.ts:

export class Estimate {
constructor(
public storyNumber: string,
public assumptions?: string,
public risks?: string,
public estimateDate?: string ) { }
}

服务本身称为ToolboxRepositoryService,方法称为getStoryEstimate():

export class ToolboxRepositoryService {
constructor(private http: HttpClient) {
this.baseUrl = "http://localhost:8080/"; 
};
getStoryEstimate(storyNumber: string) {
return this.http.get<Estimate>(this.baseUrl + "spestoryestimate/story/" + storyNumber);
}
}

目前,我的数据库拥有2条对象类型Estimate的记录,故事编号为9999和9998。我可以使用Postman测试GET请求,并按预期返回记录。但是,当我在应用程序中执行相同的GET请求时,我得到了一组不同的结果。

相关的组件方法描述如下:

getStoryEstimate(storyNumber: string){
console.log('Retrieving estimate...')
this.toolboxRepository.getStoryEstimate(storyNumber).subscribe((estimate: Estimate) => {
if(estimate.storyNumber){
this.storyEstimate.assumptions = estimate.assumptions;
this.storyEstimate.estimateDate = estimate.estimateDate;
this.storyEstimate.risks = estimate.risks;
this.storyEstimate.storyNumber = estimate.storyNumber;
}
else {
console.log('Error: story number not found!')
}
})
}
findStoryEstimate(storyNumber: string){
this.getStoryEstimate(storyNumber);
console.log(this.storyEstimate)
}

当我请求一个故事编号为9999的Estimate时,问题出现了,控制台返回一个具有未定义属性的Estimate对象。更重要的是,该对象似乎包含一个JSON对象,该对象具有的所有相关属性有问题的估计

我如何解释这个结果?或者,是否有一种方法可以访问JSON对象的属性?

您的this.toolboxRepository.getStoreEstimate是异步调用。当你是console.log(this.storeEstimate)是数据将不会返回。向订阅中输入一个日志,您将看到订单的执行方式不同。