【发布时间】:2018-10-22 20:45:15
【问题描述】:
我尝试为我的每个消息文档获取用户。我们有:
users
user_id => user_data
message
msg_id => { message, user_id }
所以我尝试了(基于that answer):
getUserData(userId) {
const docRef = this.afs.collection('/users').doc(userId);
return docRef.ref.get();
}
getMsgs(topicId){
return this.afs.collection('/topics_msgs/' + topicId + "/comments").snapshotChanges().map(actions => {
return actions.map(a => {
const commentData = a.payload.doc.data();
this.getUserData(commentData.user_id).then(
user => {
return {user: user.data(), ...commentData};
}
);
});
});
}
在组件中:
this.firebaseService.getMsgs(this.id).subscribe( msgs => {
console.log(msgs);
this.messages = msgs;
})
但它当然不能工作 - 内部映射不会返回任何超出承诺的内容,因此组件会收到 undefined 的列表。
我对如何处理这个问题有点困惑。先谢谢了。
【问题讨论】:
标签: angular firebase google-cloud-firestore observable angularfire2