Tap*_*jee 3 javascript ionic-framework google-cloud-firestore
我知道要在嵌套对象中使用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
});
}
Run Code Online (Sandbox Code Playgroud)
这应该有效:
this.afs.doc('path').update({
["count." + attr]: firebase.firestore.FieldValue.increment(1);
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1066 次 |
| 最近记录: |