为什么错误处理在nodemailer中不起作用? [英] Why won't error handling work in nodemailer?

查看:47
本文介绍了为什么错误处理在nodemailer中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用nodemailer建立一个非常简单的联系表单,它工作正常,但是我的问题是它不处理错误.如果引发错误,页面应该重定向,但是重定向不会发生,并且应用程序将停止运行.我一生无法弄清为什么会这样.这是我的代码:

I am trying to set up a really simple contact form with nodemailer and it works fine, but my issue is that it doesn't handle errors. The page should redirect if an error is thrown, but instead the redirect does not happen and the app stops running. I cannot for the life of me figure out why this is happening. Here is my code:

if (req.method === 'POST') {
  const name = req.body.name;
  const email = req.body.email;
  const msg = req.body.message;

  const transporter = nodemailer.createTransport({
    service: 'gmail',
    auth: {
      user: 'myemail', // left out here
      pass: process.env['GMAIL_PASS']
    }
  });

  const mailOptions = {
    from: 'myemail', // left out here
    to: 'myemail', // left out here
    subject: 'Portfolio Inquiry',
    text: `
          Name: ${name}
          Email: ${email}
          Message:${msg}`
  };

  transporter.sendMail(mailOptions, (error, info) => {
    if (error) {
      // If an error is thrown, it should redirect back to the page with a fail message
      return res.redirect('/about?send=fail#contact');
    } else {
      return res.redirect('/about?send=success#contact');
    }
  });
}

如果我通过注释掉一些重要的东西或者只是抛出一个错误而在脚本中引入错误,就像我说的 sendMail 回调中的错误处理块不起作用.正如我所说,它可以正常工作并发送电子邮件,但是如果出现问题,我绝对希望我的用户知道这一点.谁能帮助我了解如何解决此问题?

If I introduce an error into the script by commenting out something important or just throwing an error, as I said the error handling block in the sendMail callback doesn't do anything. As I said, it does properly work and send the email, but if something went wrong I definitely want my user to know about it. Could anyone help me understand how to correct this issue?

推荐答案

我终于想出了一个解决方案.这是一个包装函数:

I finally figured out a solution to this myself. Here is a wrapper function:

function sendEmail(req) {
    const name = req.body.name;
    const email = req.body.email;
    const msg = req.body.message;

    const transporter = nodemailer.createTransport({
        host: 'smtp.gmail.com',
        port: 587,
        secure: false,
        service: 'gmail',
        auth: {
            user: //left out,
            pass: process.env['GMAIL_PASS']
        }
    });

    const mailOptions = {
        from: //left out
        to: //left out
        subject: 'Portfolio Inquiry',
        text: `
Name: ${name}
Email: ${email}
Message:

${msg}`};

    return transporter.sendMail(mailOptions);
}

然后调用函数:

try {
   await sendEmail(req);
   return res.redirect('/about?send=success#contact')
} catch (err) {
   return res.redirect('/about?send=fail#contact')
}

由于 sendMail 函数在未提供回调的情况下会返回promise,因此可以在 try ... catch 块中调用它.

Because the sendMail function returns a promise when no callback is given, you can call it in a try...catch block.

这篇关于为什么错误处理在nodemailer中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆