显然下面的代码工作正常,但是触发器函数的执行在电子邮件发送之前很久就结束了。 不知道会不会导致错误。
延迟结束直至电子邮件发送的正确方法是什么?
const mailTransport = nodemailer.createTransport({
service: "gmail",
auth: {
user: "email",
pass: "pass"
}
});
exports.createEnrollment = functions.firestore
.document("enrollments/{enrollmentId}")
.onCreate(event => {
var newValue = event.data.data();
// ... other commands to retrieve email content and recipient
mailTransport
.sendMail(content)
.then(() => {
console.log(`New email sent to ${email}`);
})
.catch(function(error) {
console.error("Error writing document: ", error);
});
console.log("ending function");
return event.data.data();
});
最佳答案
在你的 promise 兑现之前你就要回来了。
const mailTransport = nodemailer.createTransport({
service: "gmail",
auth: {
user: "email",
pass: "pass"
}
});
exports.createEnrollment = functions.firestore
.document("enrollments/{enrollmentId}")
.onCreate(event => {
var newValue = event.data.data();
// ... other commands to retrieve email content and recipient
return mailTransport
.sendMail(content)
.then(() => {
console.log(`New email sent to ${email}`);
})
.catch(function (error) {
console.error("Error writing document: ", error);
});
console.log("ending function");
});
mailtransport.sendMail 是一个应该返回的 promise 。
但是现在你将永远不会在日志中看到结束函数..如果你愿意的话。将其移至您的 .then 函数中,或链接另一个函数。
关于javascript - Firebase 云功能在发送电子邮件之前结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48990488/