Home > database >  Firebase function taking incorrect execution path, log messages stop
Firebase function taking incorrect execution path, log messages stop

Time:09-30

I have a function, that among other things calls a function returning a boolean value to determine if the passed in date is greater than the current Date:

function isExpired(expirationDate) {
    const date =  new Date(expirationDate);
    const now =  new Date();
    const expired = (date < now);
    functions.logger.log("date expired: ", expired);
    return expired;
}

You'll notice in the helper function, I'm logging whether the date is expired or not. For some reason, that log message never appears either in the Firebase console or the CLI. I'm calling it like this:


const functions = require("firebase-functions");
var admin = require('firebase-admin');
const db = admin.database();

exports.generateCode = functions.https.onCall(async (data, context) => {
    var expirationDate;
    await joinCodeRef.once("value", snapshot => {
        expirationDate = snapshot.val().expiresOn;
    })

    if (isExpired(expirationDate)) { 
        // do some stuff
    } else {
        // return some stuff
    }
}

isExpired always evaluates to false even when the passed-in date is less than the current date. No log messages past if(isExpired) will appear in any logs either, BUT the proper values are returned in the else block meaning execution is being allowed to continue. I must be doing something wrong, but I'm not seeing it...

CodePudding user response:

Not sure if it's the cause of your problem, but you should not combine using await with using callbacks.

exports.generateCode = functions.https.onCall(async (data, context) => {
    const snapshot = await joinCodeRef.once("value");
    const expirationDate = snapshot.val().expiresOn;

    if (isExpired(expirationDate)) { 
        // do some stuff
    } else {
        // return some stuff
    }
}
  • Related