Reputation: 227
I'm using firebase and need to increment a counter so transaction
seems to be the most appropriate contender. i understand that transaction uses a promise, however i am unclear (and the manuals dont have examples) on how to use the .then
component of the promise.
At the moment in prototyping, i can 'slow it down' by using an alert like this:
var oldPosScore = firebase.database().ref('/data/n:'+stateNum[plus] + '/score/');
oldPosScore.transaction (function(increment1) {
alert ('POSITIVE statement number: ' + stateNum[plus] );
return ((increment1 || 0)*1 + 1);
});
What syntax do i use to set this up? where/how does the .then
work in this use-case?
EDIT (added data structure): Instead of firebase push keys i am using numbers so i can randomly select them.
Upvotes: 0
Views: 421
Reputation: 1420
there you can use transaction promise like below,
var oldPosScore = firebase.database().ref('/data/n:'+stateNum[plus]);
oldPosScore.child('score').transaction (function(increment1) {
return ((increment1 || 0)*1 + 1);
}).then(function(){
alert("transaction successfull");
}).catch(function(err){
alert(err);
});
see my Js Fiddle Sample for Transaction "then" Promise
I have edited my Js fiddle. See this whether it works for you or not?
Upvotes: 1