使用Angular Fire 2在Firebase中更新计数器

时间:2017-03-05 16:04:56

标签: firebase ionic2 angularfire2

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

使用固定编号更新属性对

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

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

但我想首先获得当前"投票"和"总计"属性,然后递增这些值

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

非常感谢

1 个答案:

答案 0 :(得分:0)

我不知道$ 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
      })
  })