Is there a way I can get an automatically generated identifier for a document created as part of a package using Firestore?
When using, .add()
I can easily get the identifier:
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() {
});
Is it possible to get the automatically generated identifier of the document that was added to votes
?
Update:
Doug Stevenson is right - I can access the ID in votesRef.id
source
share