我的firebase中有一个属性,需要在添加注册时增加。添加注册工作,但我似乎无法弄清楚,当注册发生时,我如何动态增加数量。 我的初始代码是:
countExperience() {
db
.collection("experiences")
.doc(this.$route.params.experience_id)
.update({ quantity: + 1 })
.then(docRef => {
console.log("added quantity");
});
},
我的问题是"更新"没有工作,它基本上将值设置为1,而不是递增。
这是工作代码:
countExperience() {
const exp_ref = db.collection("experiences").doc(this.$route.params.experience_id)
return db.runTransaction(t => {
return t.get(exp_ref).then(doc => {
const newCount = doc.data().quantity +1
t.update(exp_ref, {quantity: newCount})
})
})
},
答案 0 :(得分:0)
You need to read the value of quantity
out of the document, increment it, then write it back. What you have right now just writes the value in the document without reading it first.
You'll typically want to use a transaction to do this, so that it all happens in a transactional way.