设置一个实时侦听器以收集具有两个唯一ID Firestore的文档 [英] set a real-time listener for collection with documents with two unique ids Firestore

查看:36
本文介绍了设置一个实时侦听器以收集具有两个唯一ID Firestore的文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此功能,当页面加载并显示消息时会调用该功能.但是,我需要一个实时侦听器来仅显示第二个用户发布到集合的消息.也就是说,当user2发送一条消息并将其发布到集合中,然后在user1上进行更新

I have this function which is called when page loads and displays the messages. I, however, need a real-time listener to display the messages posted to collection from the second user only. i.e when user2 sends a message and it is posted to collection and it is then updated on user1

function fetchMessages(){
    dbRef= db.collection("user").doc("eF1RFw3FVAO3LFJ8ORrf7oJPyR13").collection('messages').orderBy("dbTime", "asc");
    dbRef.get().then((snapshot) =>{
        snapshot.docs.forEach(doc =>{
            if(doc.data().id===1){
                renderUser1Messages(doc);
            }
            else{
                renderUser2Messages(doc);
            }

        });
    });
}

我当时正在考虑删除整个函数,并将其全部做成快照侦听器.当user2发送消息时,此方法效果很好,然后将其实时显示给user1.但是,现在当user1发送一条消息时,它会显示两次.侦听器正在猜测触发并使用db中的更新值也为user1调用该函数.

I was thinking of removing the whole function and making it all a snapshot listener. This works well when user2 sends a message, it is then displayed to user1 in realtime. However, now when user1 sends a message it is displayed twice. The listener am guessing fires and calls the function for user1 too with updated values from db.

    dbRef.onSnapshot(function(snapshot) {
    snapshot.docChanges().forEach(function(change) {
     if(change.type==="added" && (change.doc.data().id==="1")){
            renderUser1Messages(change.doc);
        } else
        if(change.type==="added" && (change.doc.data().id===2)){
            renderUser2Messages(change.doc);
        }
    }); 
});

如何解决此问题

推荐答案

Firestore中可能会发生多种类型的更改,每种更改都由

There are multiple types of changes that can happen in Firestore, and each is indicated by the value of change.type:

  1. 添加了一个新文档,该文档由 added 的值指示.在这种情况下,您需要将新文档添加到您的UI.
  2. 已删除现有文档,该文档由 removed 的值指示.在这种情况下,您需要删除该文档的现有UI元素.
  3. 现有文档已更改,这由 modified 的值指示.在这种情况下,您需要更新该文档的现有UI元素.
  1. A new document is added, which is indicated by a value of added. In this case you need to add the new document to your UI.
  2. An existing document is removed, which is indicated by a value of removed. In this case you need to remove the existing UI elements for that document.
  3. An existing document is changed, which is indicated by a value of modified. In this case you'll need to update the existing UI elements for that document.

您的代码似乎只关心第3种情况,在这种情况下,文档已更新,但是您就像在添加新文档一样处理它.

Your code seems to only care about case 3, where a document is updated, but you're handling it as if a new document is added.

您将需要查找文档的现有UI元素,然后对其进行更新.或者,您可以通过删除现有的UI元素并为文档添加新的UI元素来处理它(如情况2,后跟情况1).

You'll need to instead find the existing UI element for the document, and update it. Alternative you can handle it by removing the existing UI element, and adding new UI elements for the document (so like case 2, followed by case 1).

查询方式的一个副节点:如果数据库中不仅包含文档 1 2 ,那么您的代码现在下载的文档将比您下载的更多需要.在这种情况下,我强烈建议您使用两个单独的 onSnapshot 侦听器,在您实际上关心的每个文档中一个.

A side-node on the way you query: if you have more than just document 1 and 2 in your database, your code will now download more documents than you need. I highly recommend in that case using two separate onSnapshot listeners, one on each of the documents you actually care about.

这篇关于设置一个实时侦听器以收集具有两个唯一ID Firestore的文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆