Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 为什么Firestore';s limit()是否未更新?_Reactjs_Firebase_Google Cloud Firestore - Fatal编程技术网

Reactjs 为什么Firestore';s limit()是否未更新?

Reactjs 为什么Firestore';s limit()是否未更新?,reactjs,firebase,google-cloud-firestore,Reactjs,Firebase,Google Cloud Firestore,我有一个Firestore侦听器来抓取聊天信息,我使用的限制是10条。在第一次加载时,一切正常。但是,当有10条信息且达到限制时,聊天室不接受新信息。这就好像limit函数接受前10条消息,然后当我需要用每一条新消息更新它时,它将不再接受任何消息。以下是侦听器代码: startChat() { document.getElementById("myForm").style.display = "block"; const ref = fireba

我有一个Firestore侦听器来抓取聊天信息,我使用的限制是10条。在第一次加载时,一切正常。但是,当有10条信息且达到限制时,聊天室不接受新信息。这就好像limit函数接受前10条消息,然后当我需要用每一条新消息更新它时,它将不再接受任何消息。以下是侦听器代码:

startChat() {
  document.getElementById("myForm").style.display = "block";

  const ref = firebase
    .firestore()
    .collection("Chats")
    .doc(this.state.uid)
    .collection("Messages");

  const query = ref.orderBy("timestamp", "asc").limit(10);

  this.unsubFromMessages = query.onSnapshot(
    (snapshot) => {
      console.log(
        snapshot.docs.map((doc) => {
          return doc.data();
        })
      );

      if (snapshot.empty) {
        console.log("No matching documents.");

        firebase
          .firestore()
          .collection("Chats")
          .doc(this.state.uid)
          .set({
            name: this.state.displayName,
            uid: this.state.uid,
            email: this.state.email,
          })
          .then(console.log("info saved"))
          .catch((error) => {
            console.log("Error saving info to document: ", error);
          });
      }

      snapshot.docChanges().forEach((change) => {
        if (change.type === "removed") {
          console.log(change.doc.data().content);
        } else if (change.type === "added") {
          this.setState((state) => {
            const messages = [
              ...state.messages,
              { id: change.doc.id, body: change.doc.data() },
            ];
            return {
              messages,
            };
          });

          setTimeout(this.scrollToBottom(), 2000);
        }
      });
    },
    (error) => {
      console.log(error);
    }
  );
}
有人知道为什么会发生这种情况,以及如何使limit函数接受新消息吗?谢谢

orderBy("timestamp", "asc")
使用此命令,您将返回10条最早的消息。新消息将具有更高的时间戳,因此新的第11条消息不会是最旧的10条消息的一部分

如果您想要10条最新消息,请使用降序:

orderBy("timestamp", "dsc")

尼古拉斯的回答确实解决了这个问题。但是,当聊天室关闭、启动并显示以前输入的消息时,它无法解决按错误顺序加载消息的问题。解决此问题的一种方法是向docChanges()添加reverse(),如下所示:snapshot.docChanges().reverse().forEach。希望这能帮助别人。大家好。堆积如山的岩石

尼古拉斯,谢谢你花时间回答我的问题。将“asc”更改为“desc”确实解决了加载新消息的问题。但是,在我将其更改为“asc”之前,我曾经使用过“desc”,因为当您关闭聊天并再次启动时,使用“desc”时,消息的显示顺序错误。最新消息不会显示在底部,而是显示在顶部,这不是聊天室的常见功能。你明白我的意思吗?上述情况发生在Firestore加载的消息上。但是,任何新输入的消息都会根据需要显示在底部。我想我会澄清这一点。希望这是有意义的。如果你想让我解释得更清楚,就告诉我。我知道了。我所做的是将reverse()添加到docChanges,这解决了错误顺序的问题-snapshot.docChanges().reverse().forEach。