如何检索 Firestore 中添加的文档数据?

Umi*_*aev 5 google-cloud-firestore

我有这样的代码,我可以在其中获取新插入的文档 ID:

db.collection("cities").add({
   name: "Tokyo",
   country: "Japan"
})
.then(function(docRef) {
   console.log("Document written with ID: ", docRef.id);
})
.catch(function(error) {
   console.error("Error adding document: ", error);
});
Run Code Online (Sandbox Code Playgroud)

是否可以从ref获取数据?

Gra*_*ton 1

是的,你可以这样做:

.then(function(docRef) {
   db.collection("cities").doc(docRef.id).get()
     .then(snap => {
        console.log('Here is the document you wrote to', snap.data()
      })
})
Run Code Online (Sandbox Code Playgroud)

当然,它需要您可能不想要的另一次阅读