如何在Firestore数组中增加地图值 [英] How to increment a map value in a Firestore array

查看:46
本文介绍了如何在Firestore数组中增加地图值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Firestore Firebase数据库,其中有一个集合用户集合中有一个数组,数组中有一个映射在地图中有一个数量q ..我想增加那个qty值..

I have a firestore firebase database , in which I have a collection users there is an array in the collection and in the array there is a map in map there is a field qty.. I want to increment that qty value..

使用增量无济于事,因为数量在数组索引内

using increment doesnt help as the qty is inside a array index

    db.collection("users").doc(checkId).update({
                myCart: firebase.firestore.FieldValue.arrayUnion({
                    qty: firebase.firestore.FieldValue.increment(1),

                }),

这是错误输出=>未捕获(承诺)FirebaseError:函数FieldValue.arrayUnion()用无效数据调用.FieldValue.increment()只能与update()和set()一起使用

this is the error Output => Uncaught (in promise) FirebaseError: Function FieldValue.arrayUnion() called with invalid data. FieldValue.increment() can only be used with update() and set()

推荐答案

要更新的字段嵌入在数组中.在这种情况下,您不能使用 FieldValue.increment(),因为不可能将数组元素作为命名字段值来调用.

The field you want to update is embedded in an array. In this case, you can't use FieldValue.increment(), since it's not possible to call out an array element as a named field value.

您要做的是读取整个文档,修改内存中的字段以包含所需内容,然后将该字段更新回文档中.如果您需要更新为原子操作,也可以考虑为此使用事务.

What you'll have to do instead is read the entire document, modify the field in memory to contain what you want, and update the field back into the document. Also consider using a transaction for this if you need to update to be atomic.

(如果该字段不是数组的一部分,则可以使用 FieldValue.incrememnt().)

(If the field wasn't part of an array, you could use FieldValue.incrememnt().)

这篇关于如何在Firestore数组中增加地图值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆