问题描述
我有一个网络应用程序(Angular)和一个移动应用程序(Ionic).它们共享相同的 Firestore 数据.
使用网络应用程序更新现有数据,但离子应用程序显示重复项(重新启动移动应用程序后重复项将消失),我在 Firestore 中检查了项目数据本身,它已更新且唯一.有人对此有任何线索吗?
这个问题只发生在移动应用而不是网络应用上,两者都使用"angularfire2": "^5.0.0-rc.4",
import { AngularFirestore, AngularFirestoreCollection } from 'angularfire2/firestore';this.posts$ = this.db.getRecentPosts().snapshotChanges().pipe(地图(arr => arr.map(doc => {返回 { id: doc.payload.doc.id, ...doc.payload.doc.data() }})));
做了研究,似乎(不是 100% 确定)angularfire2 问题:
由于重启后重复项消失了,其他人也报告了这个问题,我感觉问题出在 AngularFirestore
本身.作为解决方法,您可以尝试以下操作:
import { AngularFirestore, AngularFirestoreCollection } from 'angularfire2/firestore';this.posts$ = this.db.getRecentPosts().snapshotChanges().pipe(map(arr => arr.reduce((acc, doc) => {//map -> reduceconst id = doc.payload.doc.id//删除 !(id in acc) 以获取最后一个重复项!(acc 中的 id) &&acc[id] = { id, ...doc.payload.doc.data() }返回acc }}, {}//减少种子)),//你也可以 Object.values(arr.reduce(...)),但我觉得这个更易读地图(coll => Object.values(coll)));
I have a web application(Angular) and mobile application(Ionic). Both of them share the same Firestore data.
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?
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() }
}
))
);
Did research and it seems like(not 100% sure) an angularfire2 issue:AngularFirestoreCollection sometimes returns duplicate of records after inserting a new record
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中的数据本身没有重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!