Service Providers HTPP json


#1

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() {
    this.service.getData().subscribe(
      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 ?


#2

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.


#3

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() {
    this.service.getData('states').subscribe(
      data => this.tests = data,
      // err => console.log(err)
    );
    console.log(this.tests);
  }

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


#4

It needs to be inside subscribe:

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

#5

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