【发布时间】:2021-05-28 01:49:30
【问题描述】:
添加数组:
const buy = (e) => {
const washingtonRef = firebase.firestore().collection("users").doc(props.uid)
washingtonRef.update({
orders: firebase.firestore.FieldValue.arrayUnion({ id: e.target.name, quantity: +1 })
});
}
删除数组:
const delPr = (e) => {
const washingtonRef = firebase.firestore().collection("users").doc(props.uid)
washingtonRef.update({
orders: firebase.firestore.FieldValue.arrayRemove({ id: e.target.name, quantity: +1 })
});
}
是否有可能以某种方式更改数量字段?
【问题讨论】:
-
“如果你需要增加一个数组成员的值,你应该在客户端获取那个数组,获取想要的元素,更新它然后写回文档”见stackoverflow.com/a/60301382/3959488跨度>
-
所以我必须完全重写整个对象数组?
-
以 JSON 格式保存对象会更容易吗?
-
是的,如果你把它用作地图(就像你说的,一个 JSON 对象),那么你可以使用
increment方法。另一方面,您将无法再使用arrayUnion和arrayRemove方法,因为它们只适用于数组,正如其名称所示。
标签: reactjs google-cloud-firestore