Can I get the generated ID for a document created with batch().set using Firestore?

When you call doc() without any arguments, it will immediately return a DocumentReference that has a unique id, without writing anything to the database - the id is generated on the client. So if you want that id, simply use the id property on that DocumentReference. That id will become visible in the database after you've written that document.


I had similar issue, And I thing there were changes at the API of firestore.

I was getting an error for code like :

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

The change I found that was necessary is to take the ref of the doc object:

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

I hope this helps you all !