Service Providers HTPP json


formely I used this code in my service provider

getData() {
    return this.http.get(this.api + 'states').map( res => res.json());

and used this code im my page component

getData() {
      data => this.testes = data,
      err => console.log(err)

But now, I have this problem :slight_smile:
‘property json does not exist on type Object’

why ?


The new HttpClient gets JSON by default and there is no need to explicitly parse it. So you can drop the .map( res => res.json()) part of your code. See here.


thanks @pwespi, im my test I remove “json”, then :

getData() {
   return this.http.get(this.api + 'states').map( res => res );

json instead res.json, its work, but in my page1.component I following problem

tests = {};;
getData() {
      data => this.tests = data,
      // err => console.log(err)

my array come empty, if I show in console
data => console.log(data)
so, it bring me the result

I’m not getting this restult inside an object


It needs to be inside subscribe:

getData() {
    this.service.getData().subscribe((data) => {
      this.tests = data;


as incredible as it seems the problem was this I change the variable tests: any = { } in simply test; only and now work