【问题标题】:How to make it so that it sends out the pdf only after the pdf is generated using async await如何使其仅在使用 async await 生成 pdf 后才发送 pdf
【发布时间】: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


【解决方案1】:

我想通了。我没有调用该函数,因此未执行该操作。我删除了 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"
        });
      }
    })
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-17
    • 1970-01-01
    • 2021-11-14
    • 2022-07-19
    • 1970-01-01
    • 2020-12-18
    • 1970-01-01
    • 2016-09-19
    相关资源
    最近更新 更多