更新firestore数据后显示重复事件,但不会复制firestore中的数据本身

Hai*_*ang 16 firebase angularfire2 ionic3 angular google-cloud-firestore

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

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

此问题仅发生在除了Web应用程序之外的移动应用程序上,两者都使用 "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() }
        }
      ))
    );
Run Code Online (Sandbox Code Playgroud)

是否研究过(似乎并非100%肯定)angularfire2问题: AngularFirestoreCollection有时会在插入新记录后返回记录副本

在此输入图像描述

art*_*iak 5

由于重复的走了之后重新启动和其他人报告这个问题,以及它的感觉,我认为这个问题是内部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))
    );
Run Code Online (Sandbox Code Playgroud)