简体   繁体   中英

Firebase Function admin.database() null parameter in a transaction

I'm using Firebase Functions with the "Spark Plan" (free). This is part of my function:

return query.once("value").then(function(snapshot) {
snapshot.forEach(function(childSnapshot) {
  var childData = childSnapshot.val();
  if (childData.displayName === ally) {
    existAlly = true;
    console.log('uid: '+uid)
    var ref = admin.database().ref('users/'+uid).transaction(function (current_value) {
      console.log('current_value: '+uid)
      current_value.mainAlly = ally;
      current_value.coins = (current_value.coins || 0) + 10
      return current_value;
    }).then(() => {
      console.log('New Ally added');
      return true;
    });
...

Here the logs, you can see "'current_value: null'

Firebase 函数日志

But, ss you can see in the next picture, the "ref" is correct:

Firebase 数据库

So, is it a billing issue? The "admin.database()" stops working after a while? Or is it something else?

Thanks!


EDIT: I just did another test, and now the error is in "coins", with the same code:

在此处输入图片说明

Everything is working as expected. When working with transactions, you can expect that your handler function will get called the first time with null (which you will have to check for), then again with the actual contents of the database. You should review the documentation , and pay special attention to the note that says:

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 technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM