How I get the generated document ID of a collection in Firestore?
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);
});