同时调用的 Firestore 查询"onSnapshot"不起作用(



我用Ionic和Firestore创建了一个具有实时聊天功能的应用程序,但是我遇到了一个问题。

对话用方法加载:

refUneConversationMyUserCol.ref.orderBy('date', 'desc').limit(20).get()

在此基础上添加了"onSnapshot"请求检索最后一条实时发送的消息

this.unsubscribeDataUneConversation = refUneConversationMyUserCol.ref.orderBy('date', 'desc').limit(1).onSnapshot(result => {
console.log(result.docs[0].data());
if (this.isCalledBySnapshot === false) {
this.isCalledBySnapshot = true;
} else if (result.docs[0].data().expediteur !== this.authentificationService.uidUserActif) {
const data = result.docs[0].data();
const id = result.docs[0].id;
this.dataUneConversation.push({ id, ...data } as UneConversation);
}
});

它将完美地工作,然而,当我在同一时间发送消息(与2个不同的帐户相互交谈),我遇到了一个问题,onSnapshot只触发一次,我只收到一条消息。

我指定这两个消息在数据库中发送得很好,它们只是在实时会话期间不同时显示

你知道为什么吗?

谢谢

(这是整个方法)

async getDataUneConversation(idI: string) {
if (this.loadedDataUneConversation !== idI) {
/* ANCHOR Msg en direct */
this.isCalledBySnapshot = false;
if (this.unsubscribeDataUneConversation) {
await this.unsubscribeDataUneConversation();
}
const refUneConversationMyUserCol = this.afs.collection<User>('users').doc<User>(this.authentificationService.uidUserActif).collection<Conversations>('conversations');
const result = await refUneConversationMyUserCol.ref.orderBy('date', 'desc').limit(20).get();
/* ANCHOR Msg en direct */
this.unsubscribeDataUneConversation = refUneConversationMyUserCol.ref.orderBy('date', 'desc').limit(1).onSnapshot(result => {
console.log(result.docs[0].data());
if (this.isCalledBySnapshot === false) {
this.isCalledBySnapshot = true;
} else if (result.docs[0].data().expediteur !== this.authentificationService.uidUserActif) {
const data = result.docs[0].data();
const id = result.docs[0].id;
this.dataUneConversation.push({ id, ...data } as UneConversation);
}
});
/* ANCHOR Msg en brut */
if (result.docs.length < 20) {
this.infiniteLastUneConversationMax = true;
} else {
this.infiniteLastUneConversationMax = false;
}
this.infiniteLastUneConversation = result.docs[result.docs.length - 1];
this.dataUneConversation = result.docs.map(doc => {
const data = doc.data();
const id = doc.id;
return { id, ...data } as UneConversation;
});
this.dataUneConversation.reverse();
this.loadedDataUneConversation = idI;
}

}

编辑工作:

this.unsubscribeDataUneConversation = refUneConversationMyUserCol.ref.orderBy('date', 'asc').startAfter(this.dataUneConversation[this.dataUneConversation.length
- 1].date).onSnapshot(result => {
result.docs.forEach(element => {
const data = element.data();
const id = element.id;
if (!this.dataUneConversation.some(e => e.id === element.id)) {
this.dataUneConversation.push({ id, ...data } as UneConversation);
}
});
});

您将实时消息限制为只有最后一条消息。在聊天应用中,你想要收听所有的新消息。所以问题可能出在你的.limit(1)条款上。

但是如果你这样做,我知道你会得到整个对话,所有的消息,自从对话开始。

我的方法是这样的:
  1. 从您的refUneConversationMyUserCol...会话加载器获取最后一条消息的日期。
  2. 当你执行onSnapshot()获取最后一条消息时,不限制为1消息,而从最后加载消息的日期之后的日期开始。。

既然您是按日期排序的,这将是一个容易的修复。查看"在查询中添加游标">

基本上,你会对Firestore说:给我实时的新消息,但从现在开始-即使有许多消息同时发布,你也会得到它们,因为你不限制为1。

如果这还不够清楚,请随时提问。

最新更新