Is there a method to retrieve the automatically generated ID for a document that is created as part of a batch operation in Firestore?
Typically, when using .add()
, obtaining an ID is straightforward:
db.collection('posts')
.add({title: 'Hello World'})
.then(function(docRef) {
console.log('The auto-generated ID is', docRef.id)
postKey = docRef.id
});
However, when utilizing .batch()
to add documents with .doc()
and .set()
:
const batch = db.batch();
const postsRef = db.collection('posts').doc(postKey);
batch.set(postsRef, {title: 'Hello Again, World'});
const votesRef = db.collection('posts').doc(postKey)
.collection('votes').doc();
batch.set(votesRef, {upvote: true})
batch.commit().then(function() {
});
The question now arises - can we obtain the auto-generated ID for the document added to votes
collection?
Update:
According to Doug Stevenson's clarification, the ID can indeed be accessed via votesRef.id