I got this error message "[ts] Property 'then' does not exist on type 'Observable'.", how to solve it?
This is my Component :
getUsers(){
this.authService.getUsers().then((res) => {
this.user = res;
console.log(this.user);
},(err) => {
console.log(err);
});
}
This is my Service :
getUsers(){
let headers = new Headers();
headers.append('Content-Type', 'application/json');
return this.http.get('http://192.168.100.6:3000/users/data-tracer', {headers: headers})
.map(res => res.json());
}
You can call then
on a Promise
object, so because your service returns an Obaservable
object, you need to transform into a Promise
with the toPromise()
method and use catch
method to manage error in this way:
getUsers(){
this.authService.getUsers()
.toPromise()
.then((res) => {
this.user = res;
console.log(this.user);
})
.catch(err=> { console.log(err) });
}
Here you can see a DEMO. That's all.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With