How can i get value from an Rxjs observable in my code?

knowledgeseeker

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

Goga Koreli

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.

edited at
0

Comments

0 comments
Login to comment

Related

RxJs get value from observable

How can I get value from observable<void>

How can I get value of RxJs Observable in a nested array of objects to resolve?

Can I get 'this' element from rxjs observable when source is a class

With RXJS, how can I get an observable of keys pressed from the first press until the end of a time window?

How to get url as observable, so that whenever it changes, I can make possible changes in my code?

How can I get my returning value from a function in GlobalContext?

How can I get the selected value from my <select>

How to get current value of RxJS Subject or Observable?

How do I get the value of an Observable in a service from a component and pass it to my view?

How can I buffer an observable by a simple in Rxjs?

How can I complete Observable in RxJS

How to get number of ticks from rxjs Observable

How i can get last value of state? without subscribing (rxJs)

How can I chain RxJS observable in a redux-observable epic?

How can I get the value of the first observable in a chain of observables relationship

RxJS - How can I map/switch a Subject Observable and add a mean calculation to every emitted value?

Using Angular, RxJs 6 and InMemoryAPI how do I get a returned Observable value that seems to be nested in multiple observable sources?

How can I get data twice from Observable

Why I can't get input value in my javascript code

How do I get a value from a listbox instead of a textbox, my code is as follows

How can i separate my networking code from my ViewController

how I can get the value from datalist

How can I get this value from a JFileChooser?

How can I get a value from LiveData?

how can I get the value from json?

How can I get value from GroupedObservable?

How can I get value from option?

How can i get the value from json