在firestore中,我有一个属性(名为“items”)为array类型的文档。数组包含
ShoppingItem
具有以下结构的对象:
export class ShoppingItem {
id?: string;
name: string;
checked = false;
}
为了完成,在文件结构下:
export interface ShoppingList {
id: string;
name?: string;
items: ShoppingItem[]; // <-- This is the array property I am updating
}
在ui中,用户可以更新
checked
对象的属性,我要相应地更新数据库。我从firebase文档中读到我们可以使用的
arrayRemove/arrayUnion
原子删除/添加数组项。
为了更新现有的项目,我是否应该删除旧对象并每次添加新的对象(嵌套的承诺)如下?还是可以用更优雅的方式?
updateItem(listId: string, item: ShoppingItem) {
const docRef = this.db.collection("list").doc(listId)
return docref.update({ items: firestore.FieldValue.arrayRemove(item) })
.then(() => {
{
docRef.update({ items: firestore.FieldValue.arrayUnion(item) });
}
});
}
在数组中删除和添加项的另一个缺点是
闪烁
在用户界面中。