如何从firebase.firestore.DocumentSnapshot获取文档数据?

时间:2019-03-04 13:04:18

标签: angular firebase google-cloud-firestore

我从Firestore获取文档,它正在工作:

this.sub = this.beluginService.getBeluginById(this.personId)
.subscribe((documentSnapshot: firebase.firestore.DocumentSnapshot) => {
  console.log(documentSnapshot.data());
}

我的服务:

  getBeluginById(id: string) {
    return this.afs.collection('belugin').doc(id).get();
  }

在console.log中,我得到一个对象:

{ id: "ya1jibU2pZx1niGiuAmp"
  qwCF: [0, 0, 2, 0, 0, 6, 2]
  qwCO: [0, 0, 0, 0, 2, 0, 0]
  qwIMP: [10, 0, 2, 2, 2, 4, 0]
  qwME: [0, 4, 0, 4, 2, 0, 0]
  qwPL: [0, 0, 0, 0, 0, 0, 0]
  qwRI: [0, 2, 2, 2, 2, 0, 0]
  qwSH: [0, 0, 0, 0, 0, 0, 0]
  qwTW: [0, 4, 4, 2, 2, 0, 8] }

但是当我尝试获取对象属性(例如id,qwCF)时:

this.sub = this.beluginService.getBeluginById(this.personId)
.subscribe((documentSnapshot: firebase.firestore.DocumentSnapshot) => {
  this.id = documentSnapshot.data().id;
  this.qwCF = documentSnapshot.data().qwCF;
}

我无法获取data()属性。...我的VCCode显示方法data()调用错误。为什么?

1 个答案:

答案 0 :(得分:2)

如果documentSnapshot.data()在日志中起作用,也许可以从中获取数据,那就更好了:

const data = documentSnapshot.data();
this.id = data.id

可能取决于ECMA版本

this.id = data["id"];