admin管理员组

文章数量:1427458

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 mitted 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, mitted, ss) {
     if(error) {
        console.log('error: ', error);
     }
     console.log('mitted: ', mitted);
});

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 mitted 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, mitted, ss) {
     if(error) {
        console.log('error: ', error);
     }
     console.log('mitted: ', mitted);
});
Share Improve this question edited May 1, 2018 at 22:46 Doug Stevenson 319k36 gold badges456 silver badges473 bronze badges asked May 1, 2018 at 22:15 PadawanPadawan 8201 gold badge10 silver badges19 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

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;
});

本文标签: javascriptFirebase transaction returns null and completes without errorStack Overflow