Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Firestore generated doc ID after set

Is there a way to get the generated doc Id after you've saved a document to firestore?

var collection = db.collection("Users").doc();
collection.set({
                  xxx:"stuff",
                  yyy:"stuff"
               })
                .then(function (?Something?) 
                  {
                    var theIdIWant = ?Something?;
                  }
               )

enter image description here

Edit: Or just

var myID = collection.id
like image 605
Ruan Avatar asked Dec 19 '22 00:12

Ruan


1 Answers

The return value from doc() is a DocumentReference which has the unique id in its id field.

var doc = db.collection("Users").doc();
doc.set({
    xxx:"stuff",
    yyy:"stuff"
})
.then(() => { 
    var theIdIWant = doc.id
})
like image 67
Doug Stevenson Avatar answered Jan 03 '23 17:01

Doug Stevenson