Node.js 一次更新文档中多个字段的Firebase事务
我试图读取firestore文档中的一个字段,将该字段增加一个,然后沿着文档中的两个其他字段更新该字段 firebase transaction update()函数似乎只接受只有一个字段和值的JSON对象,因为当我向JSON添加其他字段时,更新失败 这项工作:Node.js 一次更新文档中多个字段的Firebase事务,node.js,firebase,transactions,google-cloud-firestore,google-cloud-functions,Node.js,Firebase,Transactions,Google Cloud Firestore,Google Cloud Functions,我试图读取firestore文档中的一个字段,将该字段增加一个,然后沿着文档中的两个其他字段更新该字段 firebase transaction update()函数似乎只接受只有一个字段和值的JSON对象,因为当我向JSON添加其他字段时,更新失败 这项工作: t.update(referralCodesDocRef,{field1:value1}) 这不起作用: t.update(referralCodesDocRef, { field1: value1, field2: value2, f
t.update(referralCodesDocRef,{field1:value1})代码>
这不起作用:
t.update(referralCodesDocRef, {
field1: value1,
field2: value2,
field3: value3
});
t.update(referralCodesDocRef, {field1: value1});
t.update(referralCodesDocRef, {field2: value2});
t.update(referralCodesDocRef, {field3: value3});
此外,这也不起作用:
t.update(referralCodesDocRef, {
field1: value1,
field2: value2,
field3: value3
});
t.update(referralCodesDocRef, {field1: value1});
t.update(referralCodesDocRef, {field2: value2});
t.update(referralCodesDocRef, {field3: value3});
下面是执行事务的函数
function runIncreaseCountTransaction(referralCodesDocRef){
return db.runTransaction(t => {
return t.get(referralCodesDocRef)
.then(doc => {
console.log(doc);
let newReferralCount = doc.data().referral_count + 1;
if(newReferralCount === max_referral_count){
const current_time_millis = Date.now();
const end_time_millis = current_time_millis+(180*1000); // ends after 3 mins
t.update(referralCodesDocRef, {referral_count: newReferralCount});
t.update(referralCodesDocRef, { timer_start_time: current_time_millis });
t.update(referralCodesDocRef, { timer_end_time: end_time_millis });
}
else{
t.update(referralCodesDocRef, { referral_count: newReferralCount });
}
return Promise.resolve(newReferralCount);
})
.then(result => {
console.log('Success: Update successful: Referral count incremented!!', result);
return true;
}).catch(err => {
console.log('Error: could not update referral count', err);
});
});
}
那么,如何使用firebase事务实现多字段更新呢?使用由多个属性组成的JavaScript对象更新文档应该没有任何问题,例如
t.update(referralCodesDocRef, {
field1: value1,
field2: value2,
field3: value3
});
问题很可能来自这样一个事实:您没有返回由事务的方法返回的事务。下面应该可以做到这一点:
function runIncreaseCountTransaction(referralCodesDocRef){
return db.runTransaction(t => {
return t.get(referralCodesDocRef)
.then(doc => {
console.log(doc);
let newReferralCount = doc.data().referral_count + 1;
if (newReferralCount === max_referral_count) {
const current_time_millis = Date.now();
const end_time_millis = current_time_millis+(180*1000); // ends after 3 mins
return t.update(referralCodesDocRef,
{
referral_count: newReferralCount,
timer_start_time: current_time_millis,
timer_end_time: end_time_millis
});
} else{
return t.update(referralCodesDocRef, { referral_count: newReferralCount });
}
});
})
.then(result => {
console.log('Success: Update successful: Referral count incremented!!', result);
return null;
})
.catch(err => {
console.log('Error: could not update referral count', err);
return null; //Note the return here.
});
}
Kotlin
您可以使用字段、值、字段、值。。。格式:
db.collection("users").doc("docID").update({
"field1", value1, // field, value
"field1, "value2" // field, value
})
如果要更新嵌套字段,有两个选项
使用点符号,如您所述:
db.collection("users").doc("docID").update({
"field1", value1,
"field2.subfield2", value2,
"field2.subfield3", value3
})
让你的“价值”成为地图:
没有一个答案是正确的(至少对我不起作用)
Kotlin
这是我的实现。非常简单:
val db = Firebase.firestore
db.collection("Users")
.document("Ronaldo")
.update("famous", true,
"distance", 5)
.addOnSuccessListener {...
.addOnFailureListener {...
所以基本上在你的第一对后再加一个逗号谢谢雷诺,你的解决方案奏效了。我所要做的就是按照你的指示用return更新代码