Angular 5 : Property 'then' does not exist on type 'Observable<any>'
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.
"then" is a promise concept, RxJS is a bit different. If you want to sequence multiple calls, then you need to use forkJoin. If you just want to have a success handler, then use subscribe and catch.
getUsers(){
this.authService.getUsers().subscribe((res) => {
this.user = res;
console.log(this.user);
}).catch((err) => {
console.log(err);
});
}
Make sure you understand the difference between promise and observable (Angular - Promise vs Observable) and make appropriate design decision.