异步 nodemailer 发送邮件 承诺

huangapple go评论93阅读模式
英文:

async nodemailer sendmail Promise

问题

如何获得一个 'then',或者异步返回一个 API 响应?
我使用 fastify,但如果在其中进行回调,它不会等待响应。
我尝试过这样做,但出现错误:TypeError: a.then is not a function

  1. const a = await transporter.sendMail(mainOptions);
  2. a.then((error, result) => {
  3. if (error) return error
  4. reply.send({
  5. messageId: result.messageId
  6. })
  7. })
英文:

how can I get a 'then', or asynchronously return an API response?<br>
I use fastify, but it doesn't wait for a response if you make a callback inside.<br>
I tried that, but the error: TypeError: a.then is not a function<br>

  1. const a = await transporter.sendMail(mainOptions);
  2. a.then((error, result) =&gt; {
  3. if (error) return error
  4. reply.send({
  5. messageId: result.messageId
  6. })
  7. })

答案1

得分: 0

以下是翻译好的部分:

简单记录 a

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

您还可以使用 try/catch 捕获错误:

  1. try {
  2. const a = await transporter.sendMail(mainOptions);
  3. console.log(a)
  4. reply.send({ messageId: result.messageId })
  5. } catch (error) {
  6. console.error(error)
  7. }

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

  1. transporter.sendMail({
  2. from: 'sender@example.com',
  3. to: 'recipient@example.com',
  4. subject: 'Message',
  5. text: 'I hope this message gets delivered!'
  6. }, (err, info) => {
  7. console.log(info.envelope);
  8. console.log(info.messageId);
  9. });
英文:

Simply log a:

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

You can also catch the error with a try/catch

  1. try {
  2. const a = await transporter.sendMail(mainOptions);
  3. console.log(a)
  4. reply.send({ messageId: result.messageId })
  5. } catch (error) {
  6. console.error(error)
  7. }

Looks like you are trying to send email with nodemailer. Have you tried to follow the documentation:

  1. transporter.sendMail({
  2. from: &#39;sender@example.com&#39;,
  3. to: &#39;recipient@example.com&#39;,
  4. subject: &#39;Message&#39;,
  5. text: &#39;I hope this message gets delivered!&#39;
  6. }, (err, info) =&gt; {
  7. console.log(info.envelope);
  8. console.log(info.messageId);
  9. });

答案2

得分: 0

请参考以下翻译的代码部分:

  1. 你应该提到完整的方法/服务逻辑以便为你提供更好的解决方案!不过,试试这个:
  2. - 确保安装 nodemailer 类型 `npm i @types/nodemailer`
  3. - 你已经在等待 transporter 发送电子邮件,不需要使用 `then()` 方法,而是尝试这样:
  4. ```typescript
  5. try {
  6. const a = await transporter.sendMail(mainOptions);
  7. if (a.error) {
  8. throw a.error;
  9. }
  10. reply.send({ messageId: a.result.messageId });
  11. } catch (error) {
  12. // 处理错误
  13. console.error(error);
  14. }
  1. <details>
  2. <summary>英文:</summary>
  3. You should mention the full method/service logic to give you a better solution! however, try this:
  4. - be sure to install nodemailer types `npm i @types/nodemailer`
  5. - you already awaiting transporter to send email, there is no need for `then()` method, try this instead:
  6. ```typescript
  7. try {
  8. const a = await transporter.sendMail(mainOptions);
  9. if (a.error) {
  10. throw a.error;
  11. }
  12. reply.send({ messageId: a.result.messageId });
  13. } catch (error) {
  14. // Handle error
  15. console.error(error);
  16. }

huangapple
  • 本文由 发表于 2023年2月24日 09:40:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75551901.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定