当您需要从数据库返回一些数据然后用它进行一些计算并将其存储回来时使用交易数据.
我们说我们的玩家列表中有一个玩家.
我们要检索属性,添加一年并将其返回Firebase.
amandaRef 正在从集合中检索年龄,然后我们可以使用交易方法.我们将获得当前年龄,添加一年并更新集合.
var ref = new Firebase('https://tutorialsfirebase.firebaseio.com'); var amandaAgeRef = ref.child("players").child("-KGb1Ls-gEErWbAMMnZC").child('age'); amandaAgeRef.transaction(function(currentAge) { return currentAge + 1; });
如果我们运行此代码,我们可以看到年龄值更新为 21 .