Get data from observabale firestoredocument

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Get data from observabale firestoredocument



I have declared below variable


userDetails$: AngularFirestoreDocument<any>;



and below in service constructor


this.userDetails$ = this.firestore.collection('users').doc('key');



How can I get actual data from userdetails$?



Thanks




2 Answers
2



use valueChanges and subscribe to the observable.


valueChanges


this.userDetails$.valueChanges().subscribe(value =>
this.item= value
);;





I tried it, its showing "Observable _isScalar: false, source: Observable, operator: MapOperator"
– Md. Parvez Alam
Aug 10 at 9:54





@Md.ParvezAlam check the update
– Sachila Ranawaka
Aug 10 at 9:55





Thanks, I got it, Just a question, the way I am doing is this recommended
– Md. Parvez Alam
Aug 10 at 10:27





yeah. but if you want to show the data using ngFor might as well use the asyn pipe without subscribing to the observable. <div *ngFor="let item of userDetails$ | async">
– Sachila Ranawaka
Aug 10 at 10:30


<div *ngFor="let item of userDetails$ | async">





this.userDetails$ = this.firestore.collection('users').doc('key').valueChanges()
– Sachila Ranawaka
Aug 10 at 10:31


this.userDetails$ = this.firestore.collection('users').doc('key').valueChanges()



You need to call valueChanges() or snapshotChanges() on it to convert it to an observable. Use snapshotChanges() if you need access to metadata.


valueChanges()


snapshotChanges()


observable


snapshotChanges()



this.firestore.collection('users').doc('key').valueChanges()


this.firestore.collection('users').doc('key').valueChanges()



or


this.firestore.collection('users').doc('key').snapshotChanges().pipe(
map(a =>
const data = a.payload.data();
const id = a.payload.id;
return id, ...data ;
)
);



Don't forget to subscribe to the created observable.


subscribe






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard