最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

如何修复功能执行顺序

运维笔记admin16浏览0评论

如何修复功能执行顺序

如何修复功能执行顺序

我正在使用pdfkit制作一个pdf文件,并将其作为附件发送到带有nodemailer的电子邮件,但它发送0字节输出.pdf。

我猜问题是在函数执行顺序中 - 它在创建pdf文件之前开始发送电子邮件。我只是不明白如何解决它...

app.post("/addressofpost", function(req, res){
  var abc = req.body.entpost;
  var subj = "pdf text"
"use strict"
const doc = new PDFDocument;
doc.pipe(fs.createWriteStream('output.pdf'));
doc.font('PalatinoBold.ttf')
   .fontSize(25)
   .text(subj, 100, 100);
doc.end();

async function main(){
  let account = await nodemailer.createTestAccount();
  let transporter = nodemailer.createTransport({
    host: "smtp settings",
    port: 465,
    secure: true,
    auth: {
      user: "mailuser",
      pass: "mailpass"
    }
  });
  let mailOptions = {
    from: '"Sender" <[email protected]',
    to: '"Receiver" <[email protected]>',
    subject: "Subject",
    text: "email text",
    html: "HTML text"
  };
    let mailOptionsPrint = {
    attachments: [
        {  
              filename: 'output.pdf'
        }],
    from: '"Sergei Dudin" <[email protected]>',
    to: '"Принтер" <[email protected]>',
    subject: "Subject",
    text: "email text",
    html: "HTML text"

  };
  let info = await transporter.sendMail(mailOptions)
  let infoPrint = await transporter.sendMail(mailOptionsPrint)
  console.log("Message sent: %s", info.messageId);
  console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info));
}
main().catch(console.error);
  console.log(abc);
  res.send('done');
 });

感谢您的任何帮助!

回答如下:

你猜对了!

在我们解决问题之前,我建议您使app.post处理程序异步,这样您就不必在该处理程序中创建另一个异步函数。

app.post("/addressofpost", async (req, res) => {
  // ...
});

另外,我建议你尽可能不混合回调和async / await。您可以为回调式异步函数创建包装函数,将它们转换为promises,然后将await转换为now-async post handler中的函数:

// I just extracted the pdf creation logic into a function
const createPdf = subj =>
  new Promise((resolve, reject) => {
    const doc = new PDFDocument();
    const writeStream = fs.createWriteStream("output.pdf");

    writeStream.on("finish", () => {
      resolve();
    });

    // TODO: handle errors and reject the promise accordingly

    doc.pipe(writeStream);
    doc
      .font("PalatinoBold.ttf")
      .fontSize(25)
      .text(subj, 100, 100);
    doc.end();
  });

与你现在可以做的那些:

app.post("/addressofpost", async function(req, res) {
  const abc = req.body.entpost;
  const subj = "pdf text";

  await createPdf(subj);

  let account = await nodemailer.createTestAccount();
  let transporter = nodemailer.createTransport({
    host: "smtp settings",
    port: 465,
    secure: true,
    auth: {
      user: "mailuser",
      pass: "mailpass"
    }
  });
  let mailOptions = {
    from: '"Sender" <[email protected]',
    to: '"Receiver" <[email protected]>',
    subject: "Subject",
    text: "email text",
    html: "HTML text"
  };
  let mailOptionsPrint = {
    attachments: [
      {
        filename: "output.pdf"
      }
    ],
    from: '"Sergei Dudin" <[email protected]>',
    to: '"Принтер" <[email protected]>',
    subject: "Subject",
    text: "email text",
    html: "HTML text"
  };
  let info = await transporter.sendMail(mailOptions);
  let infoPrint = await transporter.sendMail(mailOptionsPrint);
  console.log("Message sent: %s", info.messageId);
  console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info));

  console.log(abc);
  res.send("done");
});

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论