Angular 如何通过ID获取一个文档4+;火库

Angular 如何通过ID获取一个文档4+;火库,angular,firebase,google-cloud-firestore,Angular,Firebase,Google Cloud Firestore,如何让它正常工作?我只需要一个文件从基地id。有可能订阅它吗?因为它返回给我一个可观察的对象:( 这是我的密码 getByid(id){ return this.itemscollection.doc('9K6ue-afwwafwaf').valueChanges(); } 我认为您正在尝试这样做: constructor(db: AngularFirestore){ db.collection('yourcollection').doc("documentid").ref.get()

如何让它正常工作?我只需要一个文件从基地id。有可能订阅它吗?因为它返回给我一个可观察的对象:(

这是我的密码

getByid(id){
  return this.itemscollection.doc('9K6ue-afwwafwaf').valueChanges();
  }

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

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);
});}

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

让我们尝试将可观察到的内容转换为这样的承诺

  async getDocument(docId:string){
    let document = await this.afs.doc(docId).get().toPromise();
    return document.data();
  }

首先,您应该确保您的id是正确的。 我假设您使用了
AngularFirestore
连接到Firebase

   public this.itemsCollection = null;
   public constructor(private af: AngularFirestore) {
     this.itemCollection = this.af.collection('Your-colection-name');
   }

   public getById(docId: string): any {
     return this.itemCollection
                .doc(docId)
                .valueChanges()
                .subscribe(item => {
                  return item; 
                  // If you prefer including itemId back to object
                  // return {...item, id: docId}
                });
   }

如果您还需要文档的
id
,并使用可观察的,请执行以下操作:

this.firestore.collection('collection').doc('someid').snapshotChanges().subscribe(
      res => {
        this.item= { id: res.payload.id, ...res.payload.data() as InterfaceName };
      },
      err => {
        console.debug(err);
      }
    )

valueChanges.subscribe(value=>{return value;});
@Hareesh我尝试了它,但它没有帮助,它只返回我无法映射或forEach的订户对象:(您如何使用
getByid(id)
,请提供更多的代码我如何映射
doc.data()
到存储此数据而不包含额外字段的my class属性?请尝试js destructuring