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
‘property json does not exist on type Object’
why ?
pwespi
December 24, 2017, 6:20pm
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 .
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
pwespi
December 24, 2017, 7:43pm
4
It needs to be inside subscribe:
getData() {
this.service.getData().subscribe((data) => {
this.tests = data;
console.log(this.tests);
};
}
as incredible as it seems the problem was this I change the variable tests: any = { } in simply test; only and now work