angularfirebasegoogle-cloud-firestorenosqlangularfire2

Firestore Getting documents id from collection


I'm trying to retrieve my documents with id but can't figure it out.
Currently I retrieve my documents like this :

const racesCollection: AngularFirestoreCollection<Races> = this.afs.collection('races');
return racesCollection.valueChanges();

I do get my documents list perfectly, however there is no doc id with them.

How can I retrieve it for each document ?


Solution

  • I've finally found the solution. Victor was close with the doc data.

    const racesCollection: AngularFirestoreCollection<Race>;
    return racesCollection.snapshotChanges().map(actions => {       
      return actions.map(a => {
        const data = a.payload.doc.data() as Race;
        data.id = a.payload.doc.id;
        return data;
      });
    });
    

    ValueChanges() doesn't include metadata, therefor we must use SnapshotChanges() when we require the document id and then map it properly as stated here https://github.com/angular/angularfire2/blob/master/docs/firestore/collections.md