更新Firestore数据后显示重复事件,但Firestore中的数据本身不重复 [英] Display duplicate events after update firestore data, but the data itself in firestore isn't duplicated

查看:80
本文介绍了更新Firestore数据后显示重复事件,但Firestore中的数据本身不重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Web应用程序(Angular)和一个移动应用程序(Ionic).他们两个共享相同的Firestore数据.

I have a web application(Angular) and mobile application(Ionic). Both of them share the same Firestore data.

使用Web应用程序更新现有数据,但ionic应用程序显示重复项(重新启动移动应用程序后重复项将消失),我在Firestore中检查了项数据本身,它已更新且唯一.有人对此有任何线索吗?

Use web application update existing data but the ionic app shows duplicate items(the duplicates will be gone after restart the mobile app), I check the item data itself in Firestore, it was updated and unique. Does anyone have any clue on this?

此问题仅发生在除Web应用之外的移动应用上,它们都使用"angularfire2": "^5.0.0-rc.4",

This issue only occurs on the mobile app other than the web app, both of them use "angularfire2": "^5.0.0-rc.4",

   import { AngularFirestore, AngularFirestoreCollection } from 'angularfire2/firestore';

   this.posts$ = this.db.getRecentPosts().snapshotChanges().pipe(
      map(arr => arr.map(doc => {
          return { id: doc.payload.doc.id, ...doc.payload.doc.data() }
        }
      ))
    );

进行了研究,似乎(不是100%确信)angularfire2问题: AngularFirestoreCollection有时在插入新记录后会返回记录的重复

Did research and it seems like(not 100% sure) an angularfire2 issue: AngularFirestoreCollection sometimes returns duplicate of records after inserting a new record

推荐答案

由于重复启动后重复项已消失,其他人也报告了此问题,因此感觉对我来说,问题出在AngularFirestore本身.作为解决方法,您可以尝试以下操作:

Since duplicates are gone after restart and other people report this issue as well it feels to me that the problem is within AngularFirestore itself. As a workaround you could try the following:

 import { AngularFirestore, AngularFirestoreCollection } from 'angularfire2/firestore';

   this.posts$ = this.db.getRecentPosts().snapshotChanges().pipe(
      map(arr => arr.reduce((acc, doc) => { // map -> reduce
        const id = doc.payload.doc.id
        // remove !(id in acc) to get last duplicate
        !(id in acc) && acc[id] = { id, ...doc.payload.doc.data() } 
        return acc }
        }, {} // reduce seed
      )),
      // you can also Object.values(arr.reduce(...)), but this I find bit more readable
      map(coll => Object.values(coll))
    );

这篇关于更新Firestore数据后显示重复事件,但Firestore中的数据本身不重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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