Padawan
Padawan

Reputation: 820

Firebase transaction returns null and completes without error

I have a simple transaction which should return the count of a child node stored in my realtime database and increment it. It might be worth noting this transaction is inside of a firebase cloud onWrite function.

The issue is that the data returned is null. I know this is possible when the client data is stale however the function exits without error and committed is false.

When I use the exact same reference to retrieve a data snapshot, the correct value is returned.

var ref = admin.database(devDB).ref('/path/to/countValue');
ref.transaction(function(count) {
  if(count != null) {
     var newCount = count + 1;
     return newCount;
  }
 }, function(error, committed, ss) {
     if(error) {
        console.log('error: ', error);
     }
     console.log('committed: ', committed);
});

Upvotes: 6

Views: 3032

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317362

You can expect that the first invocation of a transaction handler will hand you a null value. This is stated in the documentation:

Transaction Function is Called Multiple Times

Your transaction handler is called multiple times and must be able to handle null data. Even if there is existing data in your database it may not be locally cached when the transaction function is run.

The problem is that your function doesn't return a value in the case where the value is null. This signals to the transaction that you want to abort the transaction. According to the API docs for transaction():

The function should return the new value it would like written (as a JavaScript object). If undefined is returned (i.e. you return with no arguments) the transaction will be aborted and the data at this location will not be modified.

The sample code in the documentation I linked earlier that performs an increment always returns a value:

upvotesRef.transaction(function (current_value) {
  return (current_value || 0) + 1;
});

Upvotes: 6

Related Questions