繁体   English   中英

如何在 firebase 9 中的现有 object 中添加新键和值而不覆盖以前的数据

[英]how to add a new key and value to an existing object in firebase 9 without overwriting previous data

我正在尝试添加到 firestore 中的 object,问题是新的 object 覆盖了以前的对象,所以在我的情况下只剩下一个 object,我放了一张数据库的图片以及我正在使用的逻辑。

 const docRef = doc(db, 'items', _authContext.currentUser.uid); await updateDoc(docRef, { itemlist: { [diffrent key each time]: arrayUnion({ name: 'item whatever')}], }, }), { merge: true }; };

在此处输入图像描述

更新嵌套字段时应使用点表示法。 此外updateDoc()不采用具有合并属性的选项(它是setDoc() )。 尝试重构代码,如下所示:

const docRef = doc(db, 'items', _authContext.currentUser.uid);
const differentKey = "someKey";

await updateDoc(docRef, {
  [`itemList.${differentKey}`]: arrayUnion({ name: "item whatever" }),
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM