简体   繁体   English

错误地调用了firestore快照之间的视图更改

[英]View changes between snapshots of firestore was called incorrectly

i am doing a toast that notifies admin about events happening in firestore but when i reload the web page the added event is called with the same number of documents?我正在敬酒,通知管理员 Firestore 中发生的事件,但是当我重新加载 web 页面时,使用相同数量的文档调用添加的事件?

 db.collection("Bill").where('UserSell', '==', ad.getAdmin).onSnapshot(
            (doc) => {
                doc.docChanges().forEach(function (a) {
                   
                    if (a.type === "added") {
                        toastr.options.progressBar = true;
                        toastr.options.showMethod = 'slideDown';
                        toastr.success('a new invoice has just been added', 'Mess')
                     }
                   }
)

but when I reload the page it shows the number of toast = number of documents even though I didn't call the click add event但是当我重新加载页面时,它会显示 toast 的数量 = 文档的数量,即使我没有调用 click add 事件

在此处输入图像描述

在此处输入图像描述

The event types are relative to the listener you attach.事件类型与您附加的侦听器相关。 So when you just attach the listener, the first time its callback is called all documents are newly added to that listener.因此,当您仅附加侦听器时,第一次调用其回调时,所有文档都会新添加到该侦听器中。 This is the expected behavior.这是预期的行为。

If you want to know whether a document is "new" by some other measure, you should include information for that purpose in the document itself.如果您想通过其他方式了解文档是否“新”,您应该在文档本身中包含用于该目的的信息。 For example, it is quite common to include timestamp fields in each document of when it was created and when it was last updated.例如,在每个文档中包含创建时间和上次更新时间的时间戳字段是很常见的。 You can then keep a timestamp in each client of when it last loaded data, and use that to create a query for documents that were created or updated since then.然后,您可以在每个客户端中保留上次加载数据的时间戳,并使用该时间戳为此后创建或更新的文档创建查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM