0

I am using Firebase Realtime to trigger Firebase Functions. The plan is to execute a transaction to change a value.

The problem is that I am only getting null values even if there is data in the location I am querying. Why?

Database content:

{ 
  "AAATest": {
    "counter": 0,
    "trigger": "b"
} 

Firebase function code



const functions = require("firebase-functions");
const admin = require("firebase-admin");


admin.initializeApp(functions.config().firebase);
const database = admin.database();


exports.transactionTest = functions.database
    .ref("/AAATest/trigger")
    .onUpdate((change, context) =>{
      
      const counterRef = database.ref("/AAATest/counter/");


      return counterRef.transaction(function(currentData) {
        console.log("INFO: ref -> " + counterRef.toString());
        console.log("INFO: CurrentDataType -> " + typeof(currentData));
        console.log("INFO: CurrentData Value -> ", currentData);
             
        if (currentData === null) {
           return; // Abort the transaction.
        } else {
          return currentData +1;
        }


      }, function(error, committed, snapshot) {
        
        if (error) {
          console.log("INFO: Transaction failed abnormally!", error);
        } else if (!committed) {
          console.log("INFO: We aborted the transaction..");
        } else {
          console.log("INFO: counter increased!");
        }


        console.log("New Value: ", snapshot.val());
      });
    });

Logs after execution

Function execution started 

INFO: ref -> https://db-test-default-rtdb.europe-west1.firebasedatabase.app/AAATest/counter

INFO: CurrentDataType -> object

INFO: CurrentData Value -> null

INFO: We aborted the transaction..

New Value: null

Function execution took 301 ms. Finished with status: ok

Manual check

If I open https://db-test-default-rtdb.europe-west1.firebasedatabase.app/AAATest/counter in the browser, it finds the value.

Capture of my browser showing data

Iroqas
  • 55
  • 7

0 Answers0