I've noticed that whenever I define a Firebase transaction in NodeJS, it always executes three times: once with no data, once with null data, and once with actual data. Is this expected or normal?
As in the following code:
firebaseOOO.child('ref').transaction(function(data) {
console.log(data);
return data;
});
outputs the following:
null
null
i1: { a1: true }
I had anticipated that it would simply print the most recent item.
Here is the same with a callback to respond to a commenter's query:
firebaseOOO.child('ref').transaction(function(data) {
console.log(data);
return data;
}, function(error, committed, snapshot) {
if (error)
console.log('failed');
else if (!committed)
console.log('aborted');
else
console.log('committed');
console.log('fin');
});
Which yields the following output:
null
null
i1: { a1: true }
committed
fin
Before asking the question, I had read the explanations of how transactions operate, thus I had tried setting applyLocally to false as follows:
firebaseOOO.child('ref').transaction(function(data) {
console.log('hit');
return data;
}, function(){}, false);
I just double-checked, but it still strikes three times, so I assumed it was something else. Getting the 'value' before transacting does "work" as intended, in that it only hits once, and that's regardless of what applyLocally is set to, so I'm not sure what applyLocally accomplishes? This is what I mean by getting the value before transacting:
firebaseOOO.child('ref').once('value', function(data) {
console.log('1');
firebaseOOO.child('ref').transaction(function(data) {
console.log('2');
return data;
});
});
Outputs:
1
2
ONCE TRANSACTION PATTERN:
firebaseOOO.child('ref').once('value', function(data) {
console.log('1');
firebaseOOO.child('ref').transaction(function(data) {
console.log('2');
return data;
});
});