Firebase:创建后获取文档快照 [英] Firebase: Get document snapshot after creating
本文介绍了Firebase:创建后获取文档快照的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道我可以创建一个新查询以通过回调中的id读取文档.但是在创建文档或至少创建了TIMESTAMP之后,我可以在回调中获取整个快照吗?
I know that I can create a new query to read the doc by id in callback. But can I get the whole snapshot in the callback after creating document or at least TIMESTAMP?
firebase.firestore().collection("comments").add({
body: data
})
.then(comment => {
console.log(comment);
})
.catch(error => {
console.log(error);
});
推荐答案
调用 CollectionRef.add(...)
返回对新创建文档的引用.为了能够访问该新文档的数据,您仍然需要加载它.所以:
Calling CollectionRef.add(...)
return a reference to the newly created document. To be able to access the data of that new document you'll need to still load it. So:
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);
});
有关工作示例,请参见: https://jsbin.com/xorucej/edit?js控制台
For a working example, see: https://jsbin.com/xorucej/edit?js,console
这篇关于Firebase:创建后获取文档快照的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文