async nodemailer sendmail Promise

问题描述 投票:0回答:2

如何获得“then”或异步返回 API 响应?
我使用 fastify,但如果你在内部进行回调,它不会等待响应。
我试过了,但是错误:

TypeError: a.then is not a function

         const a = await transporter.sendMail(mainOptions);
            a.then((error, result) => {
                if (error) return error
                reply.send({
                  messageId: result.messageId
                })
            })

javascript typescript nodemailer
2个回答
0
投票

只需登录

a

const a = await transporter.sendMail(mainOptions);
console.log(a)

您也可以使用

try/catch

捕获错误
try {
  const a = await transporter.sendMail(mainOptions);
  console.log(a)
  reply.send({ messageId: result.messageId })
} catch (error) {
  console.error(error)
}

看起来您正在尝试使用 nodemailer 发送电子邮件。您是否尝试过遵循文档:

transporter.sendMail({
    from: '[email protected]',
    to: '[email protected]',
    subject: 'Message',
    text: 'I hope this message gets delivered!'
}, (err, info) => {
    console.log(info.envelope);
    console.log(info.messageId);
});


0
投票

你应该提到完整的方法/服务逻辑,给你一个更好的解决方案!然而,试试这个:

  • 一定要安装nodemailer类型
    npm i @types/nodemailer
  • 你已经在等待运输者发送电子邮件,不需要
    then()
    方法,试试这个:
try {
 const a = await transporter.sendMail(mainOptions);
 if (a.error) {
   throw a.error;
 }
 reply.send({ messageId: a.result.messageId });
} catch (error) {
 // Handle error
 console.error(error);
}
© www.soinside.com 2019 - 2024. All rights reserved.