reactjs - 如何在 Cloud Firestore 中的数组内增加地图中的值?
问题描述
添加数组:
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 })
});
}
是否可以以某种方式更改数量字段?
解决方案
更新
您可以对 map 使用 increment 方法,但不能将其用于数组,因为无法将数组元素调用为命名字段值。
另一种方法是读取整个文档,修改内存中的数据并将字段更新回文档,例如nodejs
:
function IncrementArray(myDoc) {
let docData = _.cloneDeep(myDoc.data())
docData.orders[0].quantity += 1
docData.orders[1].quantity += 1
return docData
}
firestore.collection('users').doc(doc.id).update(
IncrementArray(doc)
)
参考:
推荐阅读
- scala - SBT:自动生成 allOutputFiles
- django - 单独渲染字段时无法访问错误
- oracle - 我在 sqlplus 中创建的表没有出现在 Navicat lite
- python - 我如何将 input() 函数放入 python 3 中的 def 函数中
- javascript - Mongoose - 如何链式保存,以便将数据保存到多个集合中
- android - 在Android上调用接口的方法时如何自动显示进度对话框?
- html - 尽管使用视口等,我的网页不会在不同的屏幕尺寸下全屏显示
- javascript - 反应:将firebase url转换为文件
- hadoop - 带有多个服务器的气流 conn_id
- r - 结合使用 nest 和 map 与 mgcv gam 和 broom augment