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 ?
To obtain the id of the documents in a collection, you must use
snapshotChanges()
Documentation https://github.com/angular/angularfire2/blob/7eb3e51022c7381dfc94ffb9e12555065f060639/docs/firestore/collections.md#example
doc.id
gets the UID.Combine with the rest of the data for one object like so:
Object.assign({ uid: doc.id }, doc.data())
Can get ID before add documents in database:
Have tried this!
but I run into this error
[ts] Spread types may only be created from object types. const data: T
For angular6+
For angular 8 and Firebase 6 you can use the option id field
this adds the Id of the document on the object with a specified key (customIdName)