Can I get the generated identifier for a document created using batch (). Set using Firestore?

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

+4
source share
2 answers

doc() - , DocumentReference, , - . , , id DocumentReference. , .

+6

, , API firestore .

:

const postsRef = db.collection('posts').doc(postKey);
batch.set(postsRef, {title: 'Hello Again, World'});

, ref doc:

const postsRef = db.collection('posts').doc(postKey).ref;

, !

0

Source: https://habr.com/ru/post/1687505/


All Articles