Home > Software engineering >  Collection Document Id in firebase
Collection Document Id in firebase

Time:04-21

How I get the generated document ID of a collection in Firestore?

Screenshot of collections and IDs

CodePudding user response:

If you want to add a document to firestore with an auto id, you can use the add method on the collection with the data of the document.

https://firebase.google.com/docs/firestore/manage-data/add-data#add_a_document

CodePudding user response:

If you want to get the id of a Firestore doc, call doc.id. This is not in doc.data and confused me as well.

const docs = await getDocs(collection(db, 'collection_name'));
docs.forEach((doc) => {
  console.log(doc.id);
});
  • Related