在服务中,我从 firebase 获取文档的 documentSnapshot。
getBelbinById(id: string) {
return this.afs.collection('belbin').doc(id).get();
}
在我订阅此服务的组件中,通过订阅获取此文档快照:
this.sub = this.belbinService.getBelbinById(this.personId)
.subscribe((documentSnapshot: firebase.firestore.DocumentSnapshot) => {
console.log(documentSnapshot.data());
}
这是我的问题开始的地方,因为我不能对数据做任何事情() - 无法访问对象的属性......对象本身具有表单并且通常显示在控制台中(
)
{ 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] }
我脑袋坏了,为什么不能访问data()属性,告诉我是什么问题?我正在尝试像这样访问:
this.sub = this.belbinService.getBelbinById(this.personId)
.subscribe((documentSnapshot: firebase.firestore.DocumentSnapshot) => {
this.id = documentSnapshot.data().id;
this.qwCF = documentSnapshot.data().qwCF;
}
在英语分支中提示。事实证明,有必要首先从 documentSnapshot 创建一个变量。之后,访问其属性。我不明白为什么需要这个迭代,但它确实有效。