I have created a script that performs a request with firebase. Since I want to avoid multiple queries on a relation at the same time, I use the transaction function as seen in the code. First, I get the current status of the relation and check if it is present or not. This seems to work. The first time is const current = null
and the function to add value to data2
will be executed.
And if I run the script the second time, then he goes into the next function successfully (doSecondFunction()
).
And unfortunately, no currentData returns to me here. I expect the value of the ref data2
that was added the first time. But as a result, I get null
.
Is there a way to work in the transaction with the current state of the database?
const ref = firebase.database().ref('data2');
ref.once('value', function(snapshot) {
const current = snapshot.val();
if(current == null){
console.log('FIRST');
addValToRefFunction();
}else{
console.log('SECOND');
doSecondFunction(current, ref);
}
});
function doSecondFunction(current, ref){
ref.transaction(function(currentData){
console.log(current); // result is the right value
console.log(currentData); // result is null ?
if (currentData === current){
console.log('Check');
return;
}
}