Angular Firestore在2上按id获取文档
如何在google firestore中通过id获取文档?是否有一些Angular Firestore在2上按id获取文档,angular,firebase,google-cloud-firestore,Angular,Firebase,Google Cloud Firestore,如何在google firestore中通过id获取文档?是否有一些get()方法?因为我搜索了一下,但没有找到适合我的答案:( 更新:this.itemscollection.doc(id).get()对我不起作用试试这段代码 this.itemscollection.doc(id).ref.get().then(函数(doc)){ 如果(文件存在){ log(“文档数据:”,doc.data()); }否则{ log(“没有这样的文档!”); } }).catch(函数(错误){ log(“
get()
方法?因为我搜索了一下,但没有找到适合我的答案:(
更新:this.itemscollection.doc(id).get()
对我不起作用试试这段代码
this.itemscollection.doc(id).ref.get().then(函数(doc)){
如果(文件存在){
log(“文档数据:”,doc.data());
}否则{
log(“没有这样的文档!”);
}
}).catch(函数(错误){
log(“获取文档时出错:”,错误);
});
成功使用angular 2(当使用包angularFire2)和firestore时,关键是要知道官方文档中操纵单个文档的所有firestore方法,如“get”“set”“update”,都是“ref”方法的子方法
举个例子
firestore - this.itemscollection.doc(id).get()
angularfirestore2 - this.itemscollection.doc(id).ref.get()
-------------------------------------
firestore - this.itemscollection.doc(id).set()
angularfirestore2 - this.itemscollection.doc(id).ref.set()
为什么你需要ref?我检查了很多示例,没有人使用它,所以我总是得到相同的错误“get不是函数”。请解释我。@sirandy你在使用angularfire2吗?@sirandy我相信angularfire2在firebase类周围创建了一个包装器。这就是为什么你需要.ref来获取doc.exists,这是firebase函数。
.ref
在我最近的测试中是不需要的。如何设置集合?它在任何文档中的位置?什么是.ref?为什么它不在任何文档中?