【发布时间】:2020-05-05 15:46:09
【问题描述】:
我无法从 Cloud Firestore 获取实时数据。如果我执行 console.log 而不是在服务中返回并简单地调用该函数(不订阅它)它就可以工作。
这是我的聊天服务文件中的代码
getRealTimeChats(): any {
this.db.collection('chats').onSnapshot(querySnapshot => {
const chat: ChatModel[] = [];
querySnapshot.forEach(doc => {
const data = doc.data() as ChatModel;
chat.push(data);
});
return chat;
});
}
下面是我的chat.ts文件中的代码
this.chatService.getRealTimeChats().subscribe((chats: ChatModel[]) => {
this.chats = chats;
});
【问题讨论】:
-
你的 getRealTimeChats 函数实际上并没有返回一个 observable。它实际上根本没有返回任何东西。 onSnapshot 回调中的 return 语句没有达到你的预期。
标签: angular firebase google-cloud-firestore observable