I'm doing an HTTP request using Angular 2, but the results aren't being displayed. I have the following service:
getData() {
return this.http.get(this.api)
.toPromise()
.then(response => response.json())
.catch(this.handleError);
}
Here's my AppComponent:
info: any;
error: any;
constructor(private appService: AppService) {}
getData() {
this.appService
.getData()
.then(response => this.info = response)
.catch(error => this.error = error);
}
ngOnInit() {
this.getData();
}
On my template, I'm calling {{info.fields.id}} but it gives me an error: ORIGINAL EXCEPTION: TypeError: Cannot read property 'data' of undefined.
Any hints on what I'm doing wrong?
I also created a Plunker to reproduce this issue.
fruit: {{info?.data?.fruit}}<div *ngIf="info">to make sure theinfois available before your code is called. The gist is to ensure the readiness of data before processing it.