【问题标题】:Display duplicate events after update firestore data, but the data itself in firestore isn't duplicated更新firestore数据后显示重复事件,但firestore中的数据本身不重复
【发布时间】:2018-12-09 01:43:03
【问题描述】:

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

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

这个问题只出现在除了网页应用之外的移动应用上,它们都使用"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 sometimes returns duplicate of records after inserting a new record

【问题讨论】:

  • 我认为你应该检查一下你是否忘记在某个地方取消订阅。

标签: angular firebase ionic3 google-cloud-firestore angularfire2


【解决方案1】:

由于重新启动后重复项消失了,并且其他人也报告了此问题,因此我感觉问题出在AngularFirestore 本身。作为一种解决方法,您可以尝试以下方法:

 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))
    );

【讨论】:

  • 感谢您的回复。目前,我自己删除重复项。我真的很想知道避免这个问题的最佳解决方案是什么。
  • 你知道了吗?每次我更新一些东西时,它都会在前端重复。这似乎已经以其他方式处理过,不是吗?
猜你喜欢
  • 1970-01-01
  • 2021-12-22
  • 2022-01-01
  • 2019-02-01
  • 2022-01-03
  • 1970-01-01
  • 2020-02-16
  • 2020-06-19
  • 2020-12-30
相关资源
最近更新 更多