Firebase Firestore查询“;onSnapshot“;同时调用不起作用(

Firebase Firestore查询“;onSnapshot“;同时调用不起作用(,firebase,ionic-framework,google-cloud-firestore,chat,real-time,Firebase,Ionic Framework,Google Cloud Firestore,Chat,Real Time,我用Ionic和Firestore创建了一个应用程序,该应用程序具有实时聊天功能,但我遇到了一个问题 会话将使用以下方法加载: refUneConversationMyUserCol.ref.orderBy('date', 'desc').limit(20).get() 此外,还添加了一个“onSnapshot”请求,用于检索最后一条实时发送的消息 this.unsubscribeDataUneConversation = refUneConversationMyUserCol.ref.or

我用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);
    }
  });
但是,当我同时发送一条消息时(两个不同的帐户相互交谈),我遇到一个问题,onSnapshot只触发一次,我只收到一条消息,这将非常有效

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

你知道为什么吗

多谢各位

(以下是整个方法)


您正在将实时消息限制为最后一条消息。在聊天应用程序中,您希望收听所有新消息。因此问题可能出在您的
.limit(1)
子句中

但如果你这样做,我知道你会得到整个对话,包括所有的信息,从对话开始

我的做法是:

  • 从您的
    refUneConversationMyUserCol…
    conversationloader获取最后一条消息的日期
  • 执行onSnapshot()以获取最后一条消息时,不要限制为1条消息,从上次加载消息的日期之后开始
  • 既然您是按日期订购的,这将是一个简单的解决方案。请查看“”

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


    请随时询问这是否不够清楚。

    您将实时消息限制为仅最后一条消息。在聊天应用程序中,您希望收听所有新消息。因此问题可能出在您的
    .limit(1)
    子句中

    但如果你这样做,我知道你会得到整个对话,包括所有的信息,从对话开始

    我的做法是:

  • 从您的
    refUneConversationMyUserCol…
    conversationloader获取最后一条消息的日期
  • 执行onSnapshot()以获取最后一条消息时,不要限制为1条消息,从上次加载消息的日期之后开始
  • 既然您是按日期订购的,这将是一个简单的解决方案。请查看“”

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


    请随意询问这是否不够清楚。

    我刚刚尝试过,经过几次修改,它工作得非常完美!非常感谢您的帮助,经过思考后,它确实更有意义。我刚刚尝试过,经过几次修改,它工作得非常完美!非常感谢您的帮助,经过思考,它确实是这样的这样做更有意义。
      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);
                  }
                });
              });