具有嵌套对象和变量字段名称的Firestore FieldValue增量不起作用 [英] Firestore FieldValue increment with nested object and variable field-name not working

查看:32
本文介绍了具有嵌套对象和变量字段名称的Firestore FieldValue增量不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道要在嵌套对象中使用 Firestore FieldValue增量函数,我需要在 update 方法中提及完整的路径.我的Ionic 5项目的主要方法采用对象字段的名称.以下是我尝试使用它的方法

I understand to use Firestore FieldValue increment function in the nested object I need to mention the completed path in the update method. My Ionic 5 project's main method takes the name of the field of the object. Following are the ways I have tried to use it

updateCounter(attr: string){
    Way1:
    let obj = {};
    obj[attr] = firebase.firestore.FieldValue.increment(1); //This is working
    obj.count[attr] = firebase.firestore.FieldValue.increment(1); //This is not working. Value is always 1
    this.afs.doc('path').update(obj);

    Way2:
    this.afs.doc('path').update({
       'count.${attr}': firebase.firestore.FieldValue.increment(1); //Creating a field called '${attr}' and not replacing the value;
    });

    Way3:
    this.afs.doc('path').update({
       `count.${attr}`: firebase.firestore.FieldValue.increment(1); //Error as '``' value not accepted in the function
    });
}

推荐答案

这应该有效:

this.afs.doc('path').update({
   ["count." + attr]: firebase.firestore.FieldValue.increment(1);
});

这篇关于具有嵌套对象和变量字段名称的Firestore FieldValue增量不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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