Is there a way that I can get the auto-generated ID for a document created as part of a batch using Firestore?
When using .add()
I can easily get an ID:
db.collection('posts')
.add({title: 'Hello World'})
.then(function(docRef) {
console.log('The auto-generated ID is', docRef.id)
postKey = docRef.id
});
Using .batch()
I can add a document 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() {
});
Can I get the auto-generated ID of the document that was added to votes
?
Update:
Doug Stevenson is correct - I can access the ID at votesRef.id