Firebase函数执行路径不正确,日志消息停止



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

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