当您需要从数据库返回一些数据然后使用它进行一些计算并将其存储回来时,将使用事务数据。
让我们说我们的玩家列表中有一个玩家。
我们想要检索属性,添加一年的年龄并将其返回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 。