【问题标题】:Update counter in Firebase with Angular Fire 2使用 Angular Fire 2 更新 Firebase 中的计数器
【发布时间】:2017-03-05 16:04:56
【问题描述】:

我正在使用 Ionic 2 和 AngularFire 2 构建应用程序 我想在用户每次点击“投票”按钮时更新一个对象。

用固定数字更新属性是没有问题的

  let userRate = 8
  this.currentBeer = this.af.database.object('/beers/' + scannedText);

  this.currentBeer.update({
    votes: 35,
    total: 642
  })

但我想先获取当前的“votes”和“total”属性,然后再递增这些值

我尝试了几个使用 .subscribe 或 .forEach 方法的选项,但我认为问题出在 observable 的 async 属性上?

非常感谢

【问题讨论】:

    标签: firebase ionic2 angularfire2


    【解决方案1】:

    我不知道 $ref 的可能性... 然后你就可以使用所有的 Firebase API

    这段代码为我完成了这项工作:

      this.currentBeer = this.af.database.object('/beers/' + scannedText);
    
      this.currentBeer.$ref.once('value')
      .then(snapshot => {
          let obj = snapshot.val()
          this.currentBeer.update({
            total: obj.total + userRating,
            votes: obj.votes + 1
          })
      })
    

    【讨论】:

    猜你喜欢
    • 2021-08-28
    • 2021-10-26
    • 2018-12-17
    • 1970-01-01
    • 2017-08-25
    • 1970-01-01
    • 1970-01-01
    • 2021-08-20
    • 1970-01-01
    相关资源
    最近更新 更多