【问题标题】:Firestore FieldValue increment with nested object and variable field-name not workingFirestore FieldValue 增量与嵌套对象和变量字段名称不起作用
【发布时间】:2020-10-28 21:25:36
【问题描述】:

我了解在嵌套对象中使用Firestore FieldValue increment 函数我需要在update 方法中提及完整路径。我的 Ionic 5 项目的 main 方法采用对象字段的名称。以下是我尝试使用它的方式

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
    });
}

【问题讨论】:

    标签: javascript ionic-framework google-cloud-firestore


    【解决方案1】:

    这应该可行:

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

    【讨论】:

    • 太棒了!非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-12
    • 1970-01-01
    • 1970-01-01
    • 2020-07-26
    • 1970-01-01
    相关资源
    最近更新 更多