Firebase 云异步错误(函数返回未定义、预期的 Promise 或值)
Firebase Cloud Async Error(Function returned undefined, expected Promise or value)
首先,提前感谢您花时间阅读本文。
我一直在为我的 Firebase 应用开发云函数,但遇到了一些令人沮丧的情况。一半时间,我的功能按预期工作;我看到了所有预期的日志,并且数据库得到了正确更新。另一半时间,功能几乎中途停止;只有一些日志出现,并且只进行了预期的数据库更新的一半。我已经调试了很长时间,但 运行 没主意了。
我一直在日志中看到的一件事是以下错误:
Function returned undefined, expected Promise or value
我不确定这个错误是否是导致上述不一致的原因,但这是我目前唯一的线索。此时,我已经注释掉了云函数中几乎所有的代码,错误似乎是由使用 async.auto
function. As soon as I remove any reference to async
引起的,错误消失了。
总结一下我的问题:
1 - 为什么使用 async.auto
会导致上述错误?
2 - 错误是与 运行 我的云函数的结果不一致的原因吗?
作为参考,这是我现在过度简化且毫无意义的函数,它会抛出上述错误:
exports.updateLeaderboard = functions.database.ref('/contests/{dateString}/ladder/dayIsComplete').onWrite((event, context) => {
const isComplete = event.after._data,
contestType = 'ladder',
dateString = context.params.dateString;
if (isComplete !== true) {
console.warn(`${contestType} for ${dateString} is not yet complete.`);
return false;
}
async.auto({
fetchWinningPicks: cb => {
return cb();
},
// ... Other stuff that I've now commented out
}, err => {
if (err) {
return false;
} else {
return true;
}
});
};
我能够通过将我的 async.auto
流程包装在 new Promise()
和 resolving/rejecting 我的最终回调中来解决错误:
exports.updateLeaderboard = functions.database.ref('/contests/{dateString}/ladder/dayIsComplete').onWrite((event, context) => {
const isComplete = event.after._data,
contestType = 'ladder',
dateString = context.params.dateString;
if (isComplete !== true) {
console.warn(`${contestType} for ${dateString} is not yet complete.`);
return false;
}
return new Promise((resolve, reject) => {
async.auto({
fetchWinningPicks: cb => {
return cb();
},
// ... Other stuff that I've now commented out
}, err => {
if (err) {
reject();
} else {
resolve();
}
});
});
};
这似乎解决了我上面的两个问题。
首先,提前感谢您花时间阅读本文。
我一直在为我的 Firebase 应用开发云函数,但遇到了一些令人沮丧的情况。一半时间,我的功能按预期工作;我看到了所有预期的日志,并且数据库得到了正确更新。另一半时间,功能几乎中途停止;只有一些日志出现,并且只进行了预期的数据库更新的一半。我已经调试了很长时间,但 运行 没主意了。
我一直在日志中看到的一件事是以下错误:
Function returned undefined, expected Promise or value
我不确定这个错误是否是导致上述不一致的原因,但这是我目前唯一的线索。此时,我已经注释掉了云函数中几乎所有的代码,错误似乎是由使用 async.auto
function. As soon as I remove any reference to async
引起的,错误消失了。
总结一下我的问题:
1 - 为什么使用 async.auto
会导致上述错误?
2 - 错误是与 运行 我的云函数的结果不一致的原因吗?
作为参考,这是我现在过度简化且毫无意义的函数,它会抛出上述错误:
exports.updateLeaderboard = functions.database.ref('/contests/{dateString}/ladder/dayIsComplete').onWrite((event, context) => {
const isComplete = event.after._data,
contestType = 'ladder',
dateString = context.params.dateString;
if (isComplete !== true) {
console.warn(`${contestType} for ${dateString} is not yet complete.`);
return false;
}
async.auto({
fetchWinningPicks: cb => {
return cb();
},
// ... Other stuff that I've now commented out
}, err => {
if (err) {
return false;
} else {
return true;
}
});
};
我能够通过将我的 async.auto
流程包装在 new Promise()
和 resolving/rejecting 我的最终回调中来解决错误:
exports.updateLeaderboard = functions.database.ref('/contests/{dateString}/ladder/dayIsComplete').onWrite((event, context) => {
const isComplete = event.after._data,
contestType = 'ladder',
dateString = context.params.dateString;
if (isComplete !== true) {
console.warn(`${contestType} for ${dateString} is not yet complete.`);
return false;
}
return new Promise((resolve, reject) => {
async.auto({
fetchWinningPicks: cb => {
return cb();
},
// ... Other stuff that I've now commented out
}, err => {
if (err) {
reject();
} else {
resolve();
}
});
});
};
这似乎解决了我上面的两个问题。