如何获得“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
})
})
只需登录
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);
});
你应该提到完整的方法/服务逻辑,给你一个更好的解决方案!然而,试试这个:
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);
}