简体   繁体   English

Node.js nodemailer - 等待新的 promise 解决错误

[英]Node.js nodemailer - Await new promise resolve error

I'm using node.js with nodemailer and I'm getting like this:我将 node.js 与 nodemailer 一起使用,我得到了这样的结果:

let mail = await new Promise((resolve,reject)=>{
  transporter.sendMail(mailOptions, (error, info) => {
    if (error) {
        console.log(error);
        reject(error);
    } else {
        res.status(200).json({ "success": true, "message": 'Thank you for your registration! We\'ve sent you an email to confirm!' });
        resolve(mail);
    }
  });
});

It is sending out mails but I'm getting this error:它正在发送邮件,但我收到此错误:

/home/project/controllers/signup.js:139
                        resolve(mail);
                                ^
ReferenceError: mail is not defined
    at transporter.sendMail (/home/project/controllers/signup.js:139:15)
    at transporter.send.args (/home/project/node_modules/nodemailer/lib/mailer/index.js:226:21)
    at callback (/home/project/node_modules/nodemailer/lib/sendmail-transport/index.js:90:28)
    at ChildProcess.sendmail.once.code (/home/project/node_modules/nodemailer/lib/sendmail-transport/index.js:131:28)
    at Object.onceWrapper (events.js:286:20)
    at ChildProcess.emit (events.js:198:13)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:248:12)
[nodemon] app crashed - waiting for file changes before starting...

Is this resolve(mail) not legitimate how I use it?这个解析(邮件)不合法我如何使用它?

let mail = await new Promise((resolve,reject)=>{
  transporter.sendMail(mailOptions, (error, info) => {
    if (error) {
        console.log(error);
        reject(error);
    } else {
        res.status(200).json({ "success": true, "message": 'Thank you for your registration! We\'ve sent you an email to confirm!' });
        // change below
        resolve(info);
    }
  });
});

The above should work for you resolving with the info which you pass to the sendMail callback.以上内容应该适用于您使用传递给sendMail回调的info来解决问题。

in the else you are passing mail witch it not defined, just replace it with info在你传递邮件的其他地方没有定义它,只需用info替换它

else {
    res.status(200).json({
          success: true,
          message: 'Thank you for your registration! We\'ve sent you an email to confirm!'
     });
     resolve(info);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM