如何通过 ID Angular 4 + Firestore 获取一份文档

Kek*_*rov 4 firebase angular google-cloud-firestore

如何让它正常工作?我只需要一个带有 id 的 base 文档。可以订阅它吗?因为它返回给我一个 Observable 对象:(

这是我的代码。

getByid(id){
  return this.itemscollection.doc('9K6ue-afwwafwaf').valueChanges();
  }
Run Code Online (Sandbox Code Playgroud)

小智 7

我认为您正在尝试这样做:

constructor(db: AngularFirestore){
db.collection('yourcollection').doc("documentid").ref.get().then(function (doc) {
  if (doc.exists) {
    console.log(doc.data());
  } else {
    console.log("There is no document!");
  }
}).catch(function (error) {
  console.log("There was an error getting your document:", error);
});}
Run Code Online (Sandbox Code Playgroud)

以上是我所知道的在 firestore 上阅读文档的最简单方法。它将显示您文档中的内部数据。我希望它对你有帮助。