Firebase 函数采用错误的执行路径,日志消息停止

Firebase function taking incorrect execution path, log messages stop

我有一个函数,它调用一个返回布尔值的函数来确定传入的日期是否大于当前日期:

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

您会在辅助函数中注意到,我正在记录日期是否过期。由于某种原因,该日志消息从未出现在 Firebase 控制台或 CLI 中。我这样称呼它:


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 始终计算为 false,即使传入的日期小于当前日期也是如此。超过 if(isExpired) 的日志消息也不会出现在任何日志中,但是在 else 块中返回正确的值意味着允许继续执行。我一定是做错了什么,但我没有看到...

不确定这是否是您的问题的原因,但您不应将 await 与回调结合使用。

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