【发布时间】:2019-05-30 08:42:34
【问题描述】:
如何让邮件传输器等到生成pdf?
我附上了我尝试过的代码。我的浏览器挂起,无法发送电子邮件。问题是邮件传输器尝试在生成 pdf 之前发送电子邮件。我猜我没有在正确的地方使用异步和等待。
async () => {
await pdf.create(document, options).then(res => {
console.log(res)
}).catch(error => {
console.error(error)
})
let message = "Testing"
let mailOptions = {
from: "xxx@gmail.com",
to: "yyy@gmail.com",
subject: 'Subject testing',
text: message,
attachments: [{
path: __dirname + '/../public/reports/' + 'test.pdf',
}]
}
await transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.log(error);
} else {
console.log("sent email")
res.json({
email: "sent"
});
}
})
}
【问题讨论】:
-
你只能
await一个承诺。你需要使用承诺。 -
谢谢你告诉我。 transporter.sendMail 中的 await 没有做任何事情。
标签: node.js express asynchronous pdf-generation sendmail