[英]How can I increment values in map fields?
我有一個 function 用於更新 Firestore 中的數據
setLocalStorage = name => () => {
...
this.database.collection(this.cookie.get('section')).doc(this.cookie.get('id')).update({
['daily.' + name]: this.database.FieldValue.increment(1),
['monthly.' + name]: this.database.FieldValue.increment(1),
['yearly.' + name]: this.database.FieldValue.increment(1)
})
}
我想在 object 映射的字段中增加我的值,但這種方法不起作用。 我該怎么做?
好的,所以我找到了一個解決方案(我不知道它是否可以,但它可以工作)。 首先,我使用正確的鍵創建新的 object,接下來我從 firestore 獲取值,將它們遞增並歸屬於之前創建的 object 鍵,然后我使用之前賦予的遞增值更新 firestore 中的值。
let incrementObj = {
daily: 0,
monthly: 0,
yearly: 0,
};
await this.database
.collection(this.cookie.get("section"))
.doc(this.cookie.get("id"))
.get()
.then((snapshot) => {
let dailyValue = snapshot.data().daily[name] + 1;
let monthlyValue = snapshot.data().monthly[name] + 1;
let yearlyValue = snapshot.data().yearly[name] + 1;
incrementObj.daily = dailyValue;
incrementObj.monthly = monthlyValue;
incrementObj.yearly = yearlyValue;
});
await this.database
.collection(this.cookie.get("section"))
.doc(this.cookie.get("id"))
.update({
["daily." + name]: incrementObj.daily,
["monthly." + name]: incrementObj.monthly,
["yearly." + name]: incrementObj.yearly,
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.