Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用角火2更新Firebase中的计数器_Firebase_Ionic2_Angularfire2 - Fatal编程技术网

使用角火2更新Firebase中的计数器

使用角火2更新Firebase中的计数器,firebase,ionic2,angularfire2,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 }) 但是我想首先得到当前的“投票数”和“总

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

使用固定的数字更新属性不会有任何问题

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

  this.currentBeer.update({
    votes: 35,
    total: 642
  })
但是我想首先得到当前的“投票数”和“总数”属性,然后增加这些值

我尝试了几个使用.subscribe或.forEach方法的选项,但我认为问题出在可观察对象的异步属性上


非常感谢

我不知道$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
      })
  })
仅供参考,您也可以使用。