我正在 Firebase 上编写一个使用实时数据库触发器的云函数。它触发 onWrite
,然后我需要使用 Promises
执行一系列命令。
当我使用一个 Promise 时,代码就会执行,而当我使用 Chain Promise 时,代码就会变得不可靠。但是,我总是在日志中看到这一点:
函数返回未定义的、预期的 Promise 或值
我在需要的地方返回值,但最终得到了这条消息。我的代码如下:
exports.sendQuotationToCustomer = functions.database.ref('/company/{companyId}/quotations/{quotationId}').onWrite((event) => {
const companyId = event.params.companyId;
console.log('Direct Generate');
const root = event.data.ref.root;
return root.child(`company/${companyId}/profile`).once('value')
.then((profileSnap) => {
console.log('1/Step');
const profile = profileSnap.val();
const docDefinition = helpers.createDocumentDefinition(profile, quotationData);
return docDefinition;
})
.then((result) => {
console.log('2/Step');
// Perform some manipulation over result. But meanwhile:
return result;
})
.catch((err) => {
console.log(`Failed with error info: ${err}`);
});
});
这里可能出了什么问题?
最佳答案
我怀疑您需要在 catch 中返回错误以避免警告:
.catch((err) => {
console.log(`Failed with error info: ${err}`);
return err;
});
如果docDefinition
未定义
,也可能发生这种情况。检查这一点并在这种情况下抛出错误可能是个好主意。
关于javascript - Firebase 函数预期的 Promise 或值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47356740/