在ReactJs中设置后获取Firestore生成的文档ID

Rua*_*uan 2 firebase reactjs google-cloud-firestore

在将文档保存到firestore后,有没有办法获取生成的文档ID?

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

在此输入图像描述

编辑:或者只是

var myID = collection.id
Run Code Online (Sandbox Code Playgroud)

Dou*_*son 8

doc()的返回值是一个DocumentReference,它的id字段中有唯一的id .

var doc = db.collection("Users").doc();
doc.set({
    xxx:"stuff",
    yyy:"stuff"
})
.then(() => { 
    var theIdIWant = doc.id
})
Run Code Online (Sandbox Code Playgroud)