Is there a way to acquire the document id that was generated after adding a document to a collection?
If I add a document to a collection that represents a \"post\"
If you want to use async/await
instead of .then()
, you can write it like this:
const post = async (doc) => {
const doc_ref = await db.collection(my_collection).add(doc)
return doc_ref.id
}
If you want to catch any errors in this function, include .catch()
:
const doc_ref = await db.collection(my_collection).add(doc).catch(err => { ... })
or you can have the calling function catch the error.