Firestore FieldValue increment with nested object and variable field-name not working

给你一囗甜甜゛ 提交于 2021-01-27 11:23:30

问题


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

回答1:


This should work:

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


来源:https://stackoverflow.com/questions/62795280/firestore-fieldvalue-increment-with-nested-object-and-variable-field-name-not-wo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!