Firestore通过id在角度2上获取文档

时间:2017-11-10 14:27:38

标签: angular firebase google-cloud-firestore

如何在google firestore中通过id获取文档?有一些get()方法吗?因为我搜索但找不到适合我的正确答案:(

更新:this.itemscollection.doc(id).get()对我不起作用

2 个答案:

答案 0 :(得分:21)

试试此代码

this.itemscollection.doc(id).ref.get().then(function(doc) {
  if (doc.exists) {
    console.log("Document data:", doc.data());
  } else {
    console.log("No such document!");
  }
}).catch(function(error) {
  console.log("Error getting document:", error);
});

答案 1 :(得分:2)

在firestore上成功使用angular 2(在使用angularFire2软件包时)的关键是要知道,官方文档中所有处理单个文档(例如“ get”,“ set”,“ update”)的firestore方法都是该工具的子级。 '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()