【问题标题】:using FieldValue in fireabase cloud functions在 firebase 云函数中使用字段值
【发布时间】:2020-06-03 07:33:31
【问题描述】:

admin.firestore.FieldValue.increment(1) 是否以原子方式实现,这意味着我可以使用它而不是使用事务(比如增加点赞数)

谢谢

【问题讨论】:

    标签: firebase google-cloud-firestore google-cloud-functions


    【解决方案1】:

    是的,您可以使用admin.firestore.FieldValue.increment(1) 代替事务。

    实际上在 Transactions 的文档中提到过,请参阅 here 和下面我粘贴文档内容的位置(请参阅代码中的以下注释:“注意:这可以通过更新人口在没有事务的情况下完成使用FieldValue.increment()")

    // Initialize document
    let cityRef = db.collection('cities').doc('SF');
    let setCity = cityRef.set({
      name: 'San Francisco',
      state: 'CA',
      country: 'USA',
      capital: false,
      population: 860000
    });
    
    let transaction = db.runTransaction(t => {
      return t.get(cityRef)
        .then(doc => {
          // Add one person to the city population.
          // Note: this could be done without a transaction
          //       by updating the population using FieldValue.increment()
          let newPopulation = doc.data().population + 1;
          t.update(cityRef, {population: newPopulation});
        });
    }).then(result => {
      console.log('Transaction success!');
    }).catch(err => {
      console.log('Transaction failure:', err);
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-20
      • 2019-01-18
      • 2017-12-20
      • 2017-11-02
      • 1970-01-01
      • 2021-10-18
      • 2019-11-30
      • 1970-01-01
      相关资源
      最近更新 更多