【问题标题】:Firebase transaction to update multiple fields in a document at onceFirebase 事务一次更新文档中的多个字段
【发布时间】:2020-08-30 01:11:54
【问题描述】:

我正在尝试读取 Firestore 文档中的单个字段,将该字段递增 1 并在文档中的其他两个字段旁边更新该字段。

Firebase 事务更新()函数似乎接受一个只有一个字段和值的 JSON 对象,因为当我将其他字段添加到 JSON 时,更新失败。

这行得通:

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

这是执行交易的函数

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 事务实现多个字段更新?

【问题讨论】:

    标签: node.js firebase transactions google-cloud-firestore google-cloud-functions


    【解决方案1】:

    使用由多个属性组成的 JavaScript 对象来更新文档应该完全没有问题,例如

    t.update(referralCodesDocRef, {
    field1: value1,
    field2: value2,
    field3: value3
    });
    

    问题很可能来自您没有返回由事务的update() 方法返回的事务。以下应该可以解决问题:

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

    【讨论】:

    • 感谢 Renaud,您的解决方案奏效了,我所要做的就是按照您的指示使用 return 更新代码
    【解决方案2】:

    没有一个答案是正确的(至少对我没有用)。

    科特林

    这是我的实现。很简单:

    val db = Firebase.firestore
                    db.collection("Users")
                        .document("Ronaldo")
                        .update("famous", true,
                            "distance", 5)
                        .addOnSuccessListener {...
                        .addOnFailureListener {...
    
           
    

    所以基本上在你的第一对之后添加另一个逗号

    【讨论】:

      【解决方案3】:

      科特林
      你可以update一个Firestore文档中的多个字段使用字段、值、字段、值...格式:

      db.collection("users").doc("docID").update({
          "field1", value1, // field, value
          "field1, "value2" // field, value
      })
      

      如果您想更新嵌套字段,您有几个选择。

      1. 使用你提到的点符号:

        db.collection("users").doc("docID").update({
          "field1", value1,
          "field2.subfield2", value2,
          "field2.subfield3", value3
         })
        
      2. 让你的“价值”成为一张地图:

        db.collection("users").doc("docID").update({
         "field1", value1,
         "field2", mapOf(
             "field3" to value3,
             "field4" to value4
        })
        

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-02
        • 2019-10-14
        • 2022-11-25
        • 2019-07-20
        • 2018-05-11
        • 2020-07-17
        • 2021-12-19
        • 1970-01-01
        相关资源
        最近更新 更多