Reputation: 815
I am trying to send an email using mailgun and firebase cloud functions. The mail IS sent, but still returns an error that crashes my app. I believe this is caused by the error: TypeError: Cannot read property 'catch' of undefined at exports.sendReportRequest.functions.https.onRequest (/srv/lib/index.js:57:9)
In my code, I am using mailgun's mailgun-js module to send the email, and I have copied code straight from the documentation. However, firebase requires that I "handle promises correctly", ie uses .catch to catch any errors. However, once deployed, it seems that firebase doesn't recognize .send() as a promise, and throws an error.
This is for a flutter application, and whenever I run the cloud function, my app crashes, and the functions throws an error in the firebase logs.
I am already using the blaze plan, so external API calls work. In fact, the email IS sent, but still throws an error that crashes my app. There are no problems with domain and apikey, else email wouldnt be sent.
I have tried using onRequest, onCall and trigger functions, but they all throw the same error.
I've tried returning the promise and not returning, but to no avail. Right now I am not returning anything (or returning void).
// MailGun cloud function
export const sendReportRequest = functions.https.onCall((data, context) => {
console.log("REPORT SENT");
const sendData = {
from: 'Excited User <[email protected]>',
to: '[email protected]',
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); });
});
Error
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:
Upvotes: 0
Views: 1989
Reputation: 1721
That is because send is probably not returning a promise, it uses the callback method instead. you can notice that because you pass it a function which recieves the returning data in the line mg.messages().send(sendData, *(error, body)* => {
.
This means there is no catch
phrase that can come after it. You get the error inside the callback function , so just handle it there, or if u really want you can wrap it with try and throw out the error, than catch it outside, like this:
try {
mg.messages().send(sendData, (error, body) => {
if (error) { throw new Error(error); }
console.log("message sent:", body);
})
} catch(e) {
// handle error
}
As a side note, I went to mailgun-js github repo to try and get you a documentation example, and i saw it is archived and no longer supported... you might wanna consider using another library :)
Also, here is a nice article I found which explains pretty good the whole callbacks / promises / async-await mess, what are the differences and how and when to use each, if you want to read more.
Upvotes: 1