I'm retrieving data from an API. The API returns an object with various properties. I would like to be able to access the properties of the object, but at the moment only the first property is accessible. When I console.log the observable from the service I see the object and all the properties. However, when I console.log the object from the subscribe in the component it returns an empty array. As a result the first property is returned in the component, but I can't access anything else.
I've also tried accessing the data from the observable and returning exactly the property I want, however when I do so it silently fails. Can someone please tell me what I'm doing wrong? Below is the relevant code.
Component:
getPrice() {
this.data.getData(this.API_Price)
.subscribe(
price => this.price = price,
error => this.errorMessage_price = <any>error);
console.log('component', Object.keys(this.price));
}
Service:
getData(url): Observable<any> {
this.showLoader();
return this.http.get(url)
.map(this.extractData)
.catch(this.handleError)
.finally(() => {
this.hideLoader();
});
}
public extractData(res) {
const body = res.json();
console.log('service', body);
return body;
}
public handleError(error: any) {
const errMsg = (error.message) ? error.message :
error.status ? `${error.status} - ${error.statusText}` : 'Server error';
this.showLoader();
console.error('Error', errMsg); // log to console instead
return Observable.throw(errMsg);
}