使用角火2更新Firebase中的计数器
我正在用Ionic 2和AngularFire 2构建一个应用程序 我想在用户每次点击“投票”按钮时更新一个对象 使用固定的数字更新属性不会有任何问题使用角火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 }) 但是我想首先得到当前的“投票数”和“总
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
})
})
仅供参考,您也可以使用。