Hi All I have a small snippet of code which is not compiling. i am using angular 5 , rxjs and typescript in my code.
the error is unable to assign this.account = value . it says value is Observable and it cannot be assigned to this.account which is an Account object.
this.authenticationService.currentUser$.pipe(
map( user => user.account.id),
map( id => this.accountService.get(id) )
).subscribe(
value => { this.account = value }
);
any idea how can i rewrite the code snippet to get it work ? thank you so much
As I understand this.accountService.get(id)
returns Observable, in that case you need to use switchMap()
instead of map()
operator, otherwise you will get Observable inside subscribe instead of the value that you are interested in.
Your example will become:
this.authenticationService.currentUser$.pipe(
map(user => user.account.id),
switchMap(id => this.accountService.get(id)),
).subscribe(
value => { this.account = value }
);
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments