node.js - 如何使其仅在使用 async await 生成 pdf 后才发送 pdf
问题描述
如何使邮件传输器等到生成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"
});
}
})
}
解决方案
我想到了。我没有调用该函数,因此未执行该操作。我删除了 transporter.sendMail 中的等待,因为它没有做任何事情。
sendPDF();
async function sendPDF() {
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',
}]
}
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.log(error);
} else {
console.log("sent email")
res.json({
email: "sent"
});
}
})
}
推荐阅读
- c++ - C++ 代码中使用的 C 结构生成错误“调用隐式删除...”
- clojure - Clojure 规范 - 向用户显示好的错误消息的方法
- html - 如何使用纯 CSS 沿 HTML 选择中的选定选项附加一个勾号图标
- react-native - 我在使用 expo 应用程序在物理设备上进行 react-native 工作时遇到了一些问题
- javascript - 如何让我的输入值显示在我的应用程序中,而不是显示为警报?
- apache-kafka - 如何在 Apache Beam 中使用带有 protobuf 定义的 Kafka 消息?
- javascript - 需要保留原始数组值并创建副本,在副本中更改一个值但原始值也被更改?
- stripe-payments - Stripe 的价格计算信息到客户端应用程序
- java - 独立于平台的方式为进程启动单独的 JVM?
- python - Beautiful Soup 只找到了一半的通缉表