Search code examples
google-cloud-firestoreangular8angularfire2

firestore document id and user created field called id


I am working on a angular 8 project and firestore collection with multiple documents. Each document has a field named called id. When I use curly bracket interpolation to display the id from the document (something like {{data.id}}) it is always displaying the auto-generated document id instead of the user-created id field in the document. Other document values like name are fetched properly (e.g. {{data.name}}). How do I get the id value I want and not the document id? Thank you in advance.


Solution

  • If you were using the default node.js Firestore SDK you would do it as follows:

    const doc = await db.collection('mycollection').doc('XXXXXX').get();
    console.log(doc.id) // returns default Firestore document id
    console.log(doc.data().id) // returns the value of documents field id
    

    From what I could see of Angularfire2 library they do wrappings and conversions over the documents and probably that's what is causing the document id field to be overwritten by the Firestore UUID doc id. It seems that if you're using the valueChanges method metadata is trimmed and you should be able to access the id field. If you're using the SnapshotChanges method probably you can hack something. Sorry for not being able to post an actual code solution but hope this helps somewhat.