0

product-service.ts

provinsi():Observable<any> {
return this.http.get('http://127.0.0.1:8000/api/provinsi').pipe(
map(this.extractData),
catchError(this.handleError));
}
public extractData(res: Response) {
let body = res;
return body || {};
}

restaurant.ts

provinsi() {
    this.pservice.provinsi()
        .subscribe(data => this.provinsis = data.results);
}

json

{"results":[{"province_id":"1","province":"Bali"},{"province_id":"2","province":"Bangka Belitung"} . . .

i need a way to extract my json to angular / ionic view , when i run my apps, still appear "runtime error"

Azizal Yunan P
  • 53
  • 1
  • 1
  • 9

1 Answers1

1

Your issue is in extractData. You are returning a Response object so when you are using that you do not have the expected json. To solve this, get the json from the Response object using the .json() method:

 public extractData(res) {
    let body = res.json();
    return body || {};
 }

Tip: You can use a ES6 feature called destructuring when you access to a json property. e.g:

provinsi() {
    this.pservice.provinsi()
        .subscribe(({ results }) => this.provinsis = results);
}

UPDATE: If you want to work with TypeScript classes, you need to import the Response class from @angular/http:

import { Http, Response } from '@angular/http';

This class was deprecated. You can check the new usage in this question here

Sergio Escudero
  • 1,798
  • 14
  • 21