Getting "TypeError: Cannot read property 'catch' of undefined" when executing a mailgun cloud function in firebase
Getting "TypeError: Cannot read property 'catch' of undefined" when executing a mailgun cloud function in firebase
我正在尝试使用 mailgun 和 firebase 云功能发送电子邮件。邮件已发送,但仍然 returns 导致我的应用程序崩溃的错误。我相信这是由错误引起的:
类型错误:无法读取未定义的 属性 'catch'
在 exports.sendReportRequest.functions.https.onRequest (/srv/lib/index.js:57:9)
在我的代码中,我使用 mailgun 的 mailgun-js 模块发送电子邮件,并且我直接从文档中复制了代码。但是,firebase 要求我 "handle promises correctly",即使用 .catch 来捕获任何错误。但是,一旦部署,firebase 似乎无法将 .send() 识别为承诺,并引发错误。
这是针对 flutter 应用程序的,每当我 运行 云函数时,我的应用程序就会崩溃,并且这些函数会在 firebase 日志中抛出错误。
我已经在使用 blaze 计划,因此外部 API 调用有效。事实上,电子邮件已发送,但仍会引发导致我的应用程序崩溃的错误。 domain和apikey都没有问题,不然发不了邮件
我尝试过使用 onRequest、onCall 和 trigger 函数,但它们都抛出相同的错误。
我试过还诺不还,但是没有用。现在我没有返回任何东西(或返回无效)。
// MailGun cloud function
export const sendReportRequest = functions.https.onCall((data, context) => {
console.log("REPORT SENT");
const sendData = {
from: 'Excited User <me@samples.mailgun.org>',
to: 'xxxx@gmail.com',
subject: 'Hello',
text: 'Testing some Mailgun awesomeness!'
};
mg.messages().send(sendData, (error, body) => {
if (error) { console.log("error!", error); }
console.log("message sent:", body);
})
.catch((err) => { console.log("error", err); });
});
错误
TypeError: Cannot read property 'catch' of undefined
at Object.<anonymous> (/srv/lib/index.js:55:9)
at Generator.next (<anonymous>)
at /srv/lib/index.js:7:71
at new Promise (<anonymous>)
at __awaiter (/srv/lib/index.js:3:12)
at exports.sendReportRequest.functions.https.onRequest (/srv/lib/index.js:43:69)
at cloudFunction (/srv/node_modules/firebase-functions/lib/providers/https.js:57:9)
at /worker/worker.js:783:7
at /worker/worker.js:766:11
at _combinedTickCallback (internal/process/next_tick.js:132:
那是因为 send 可能没有返回承诺,而是使用回调方法。您会注意到,因为您向它传递了一个函数,该函数接收 mg.messages().send(sendData, *(error, body)* => {
.
行中的返回数据
这意味着它后面没有 catch
短语。你在回调函数中得到错误,所以就在那里处理它,或者如果你真的想要你可以用 try 包装它并抛出错误,而不是在外面捕获它,就像这样:
try {
mg.messages().send(sendData, (error, body) => {
if (error) { throw new Error(error); }
console.log("message sent:", body);
})
} catch(e) {
// handle error
}
附带说明一下,我去了 mailgun-js github 仓库试图给你一个文档示例,我看到它已存档并且不再受支持......你可能想考虑使用另一个图书馆:)
此外,here 是我发现的一篇很好的文章,它很好地解释了整个回调/承诺/异步等待混乱,它们有什么区别以及如何以及何时使用它们,如果您想阅读更多
我正在尝试使用 mailgun 和 firebase 云功能发送电子邮件。邮件已发送,但仍然 returns 导致我的应用程序崩溃的错误。我相信这是由错误引起的: 类型错误:无法读取未定义的 属性 'catch' 在 exports.sendReportRequest.functions.https.onRequest (/srv/lib/index.js:57:9)
在我的代码中,我使用 mailgun 的 mailgun-js 模块发送电子邮件,并且我直接从文档中复制了代码。但是,firebase 要求我 "handle promises correctly",即使用 .catch 来捕获任何错误。但是,一旦部署,firebase 似乎无法将 .send() 识别为承诺,并引发错误。
这是针对 flutter 应用程序的,每当我 运行 云函数时,我的应用程序就会崩溃,并且这些函数会在 firebase 日志中抛出错误。
我已经在使用 blaze 计划,因此外部 API 调用有效。事实上,电子邮件已发送,但仍会引发导致我的应用程序崩溃的错误。 domain和apikey都没有问题,不然发不了邮件
我尝试过使用 onRequest、onCall 和 trigger 函数,但它们都抛出相同的错误。
我试过还诺不还,但是没有用。现在我没有返回任何东西(或返回无效)。
// MailGun cloud function
export const sendReportRequest = functions.https.onCall((data, context) => {
console.log("REPORT SENT");
const sendData = {
from: 'Excited User <me@samples.mailgun.org>',
to: 'xxxx@gmail.com',
subject: 'Hello',
text: 'Testing some Mailgun awesomeness!'
};
mg.messages().send(sendData, (error, body) => {
if (error) { console.log("error!", error); }
console.log("message sent:", body);
})
.catch((err) => { console.log("error", err); });
});
错误
TypeError: Cannot read property 'catch' of undefined
at Object.<anonymous> (/srv/lib/index.js:55:9)
at Generator.next (<anonymous>)
at /srv/lib/index.js:7:71
at new Promise (<anonymous>)
at __awaiter (/srv/lib/index.js:3:12)
at exports.sendReportRequest.functions.https.onRequest (/srv/lib/index.js:43:69)
at cloudFunction (/srv/node_modules/firebase-functions/lib/providers/https.js:57:9)
at /worker/worker.js:783:7
at /worker/worker.js:766:11
at _combinedTickCallback (internal/process/next_tick.js:132:
那是因为 send 可能没有返回承诺,而是使用回调方法。您会注意到,因为您向它传递了一个函数,该函数接收 mg.messages().send(sendData, *(error, body)* => {
.
这意味着它后面没有 catch
短语。你在回调函数中得到错误,所以就在那里处理它,或者如果你真的想要你可以用 try 包装它并抛出错误,而不是在外面捕获它,就像这样:
try {
mg.messages().send(sendData, (error, body) => {
if (error) { throw new Error(error); }
console.log("message sent:", body);
})
} catch(e) {
// handle error
}
附带说明一下,我去了 mailgun-js github 仓库试图给你一个文档示例,我看到它已存档并且不再受支持......你可能想考虑使用另一个图书馆:)
此外,here 是我发现的一篇很好的文章,它很好地解释了整个回调/承诺/异步等待混乱,它们有什么区别以及如何以及何时使用它们,如果您想阅读更多