gpt4 book ai didi

javascript - Firebase 云功能在发送电子邮件之前结束

转载 作者:行者123 更新时间:2023-12-03 02:24:50 25 4
gpt4 key购买 nike

显然下面的代码工作正常,但是触发器函数的执行在电子邮件发送之前很久就结束了。不知道会不会导致错误。

延迟结束直至电子邮件发送的正确方法是什么?

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com