Firebase:创建后获取文档快照

ren*_*dom 1 javascript firebase google-cloud-firestore

我知道我可以创建一个新查询来通过回调中的 id 读取文档。但是我可以在创建文档或至少 TIMESTAMP 之后在回调中获取整个快照吗?

firebase.firestore().collection("comments").add({
  body: data
})
.then(comment => {
  console.log(comment);
})
.catch(error => {
  console.log(error);
});
Run Code Online (Sandbox Code Playgroud)

Fra*_*len 5

调用CollectionRef.add(...)返回对新创建文档的引用。为了能够访问该新文档的数据,您仍然需要加载它。所以:

firebase.firestore().collection("48486654").add({
  timestamp: firebase.firestore.FieldValue.serverTimestamp()
})
.then(function(docRef) {
  docRef.get().then(function(doc) {
    console.log(doc.data().timestamp.toString());
  });
})
.catch(function(error) {
  console.error(error);
});
Run Code Online (Sandbox Code Playgroud)

有关工作示例,请参阅:https : //jsbin.com/xorucej/edit?js,console