尝试从 Angular 中的 firestore 获取单个文档时出错

Error when trying to fetch single document from firestore in Angular

我想我在尝试创建一个函数以从 Angular 中的 firestore 获取单个文档的数据时犯了一些愚蠢的错误。

public async getPage(id){
    const ref = this.db.collection('pages').doc(id).get().data();
  }

这是我的原始代码。我没有比获取更进一步,因为我得到了错误: Property 'data' does not exist on type 'Observable<DocumentSnapshot<DocumentData>>'

我也试过以下,同样的错误:

public async getPage(id){
    const ref = await this.db.collection('pages').doc(id).get().data();
  }
public async getPage(id){
     const result = await this.db.collection('pages').doc(id).get()
     console.log(result.data())
}

您必须订阅可观察对象 get() 或使用 toPromise() 转换为承诺以等待调用。

export class PageServices {
  public getPage(id: string) {
    this.db.collection('pages').doc(id).get().subscribe((res => {
      console.log(res.data());
    }));
  }

  // or using `toPromise`
  public async getPageAsync(id: string) {
    const docSnapshot = await this.db.collection('pages').doc(id).get().toPromise();
    if (docSnapshot.exists) {
      console.log(docSnapshot.data());
    }
  }
}