In my Angular application, I am using a service to send GET requests and retrieve data from a MongoDB database. The service includes a method that searches for a custom object called an Estimate with specific properties:
Estimate.model.ts:
export class Estimate {
constructor(
public storyNumber: string,
public assumptions?: string,
public risks?: string,
public estimateDate?: string ) { }
}
The service, ToolboxRepositoryService, has a method named 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);
}
}
Currently, there are 2 records in my database of Estimates with story numbers 9999 and 9998. Testing the GET requests through Postman returns the expected results. However, running the same request in the application yields different outcomes.
The relevant component methods are outlined below:
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)
}
An issue arises when requesting an Estimate with story number 9999, leading to undefined properties in the returned object. Furthermore, the object seems to contain a JSON object with all the necessary attributes of the Estimate being queried.
How should I interpret this outcome? Is there a way to access the attributes of the JSON object?