angulargoogle-cloud-firestoreangularfire5

How to get the particular document id? AngularFire 5.1.1 | Cloud Firestore | Documents


constructor(private afs: AngularFirestore) {
    this.itemDoc = afs.doc<Item>('items/id');
    this.item = this.itemDoc.valueChanges();
}

Now, this.item has the value of a particular document except id of the document.

I want id and document data.

(Or)

If the snapshotChanges() is the way, how will I use this?

Thank you..!


Solution

  • You can't get the document ID from valueChanges. As its name implies, it only exposes the value of the document, not its metadata.

    To get the document ID, you will need to observe snapshotChanges instead. Since the snapshotChanges exposes a DocumentChangeAction, you should be able to get the document ID from this.itemDoc.payload.doc.id.