4

From my understanding of Transactions, it can return null for two reasons:

  1. There is actually no value at the node where the transaction is being performed.
  2. The local cache is empty as Firebase Cloud Functions is stateless. Therefore, it may return null the very first time and it will re-run the function.

My question is, how do we distinguish between these two cases? Or does firebase do the distinction by itself?

Myref.transaction(function(currentData) {
    if(currentData != null) {
        return currentData + 1;
    } else {
        console.log("Got null")
        return;
    }
}, function(error, committed, snapshot) {
    if(!committed) {
        // The transaction returned null. 
        // But don't know if it's  because the node is null or 
        // because the transaction failed during the first iteration.
    }
});

In the above example, the transaction callback will be passed null both when the value at Myref is non-existent and when it attempts to get the data in the very first try when executing the transaction.

If the value of Myref is actually empty, I want the number 1238484 to be filled in there. If it is not, and the null is actually being thrown because of a wrong read by the transaction, how do I make this distinction?

PS: Please don't suggest a listener at the node. Is there any other more effective way of doing this?

ABakerSmith
  • 22,759
  • 9
  • 68
  • 78
Arjun Ram
  • 369
  • 2
  • 18
  • What is the specific problem you're trying to solve with a transaction? – Doug Stevenson Feb 11 '20 at 17:11
  • 3
    This is for the sake of a better understanding of transactions. I've simplified the example now. With the code that I've written, I want the transaction to be aborted if there is no value at the node. But the problem is that it gets aborted even if there is a value at the node due to the nature of transactions itself. – Arjun Ram Feb 12 '20 at 04:22
  • Are you verifying that node.exists() returns true, as this will help you to determine if it's the node exists, therefor you are not calling it on a null place. – Soni Sol Feb 25 '20 at 02:53
  • In my PS, I've asked for a way other than checking if node.exists(). – Arjun Ram Feb 26 '20 at 02:26

1 Answers1

3

On initial run of the transaction and value returned from the update function is undefined, onComplete is invoked with error to be null

For subsequent runs when there is no data, a reason for aborting the transaction is provided.

You can check the value of error to differentiate whether there is no data at the reference or local cache is empty.

error === null && !committed // local cache is empty
error !== null && !committed // there is no data

This is internal implementation detail and you shouldn't rely on it for your queries.

Oluwafemi Sule
  • 36,144
  • 1
  • 56
  • 81
  • So what would you do if there is actually data in the database ref but it still returns null in first run as you said ?, how can I overcome this issue? I receive null from the transcation but it is not supposed to return null I assume it is happening because of the local cache thingy that you mentioned – Yarin Shitrit Feb 21 '21 at 16:41