nodemailer-express-handlebars | Error: ENOENT: no such file or directory for template

nodemailer-express-handlebars | Error: ENOENT: no such file or directory for template

我遇到 nodemailer-express-handlebars 输出错误的问题:

[Error: ENOENT: no such file or directory, open ''] {
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path: '/Users/person/Code/app/api/controllers/templates/undefined.hbs'

我的设置如下:

 const handlebarOptions = {
    viewEngine: {
      extName: ".hbs",
      partialsDir: path.resolve(__dirname, "templates"),
      defaultLayout: false
    },
    viewPath: path.resolve(__dirname, "templates"),
    extName: ".hbs"
  };

  transporter.use('compile', hbs(handlebarOptions));

并发送电子邮件:

 let mailOptions = await transporter.sendMail({
    from: '"Test - No Reply" <test@test.com>'
    to: 'someEmail@gmail.com, 
    subject: "Hello ✔",
    template: 'welcome',
  });

奇怪的是,我仍然收到电子邮件,尽管它说找不到文件。如何解决此错误并让 nodemailer-express-handlebars 看不到文件 'undefined.hbs'?

更新:

我在 nodemailer 中将 'welcome' 更改为 'welcome.hbs',现在错误是找不到 'welcome.hbs.hbs'。这是有道理的,你会认为解决方案是删除 '.hbs' 并使其成为 'welcome' 但随后我们又回到了 'undefined.hbs'.

的原始错误

此外,如果我将模板更改为 'welcome2',它会说找不到 'welcome2.hbs'。很奇怪...好像只有当模板文件与文件名匹配时它才变得未定义。

它不应该是模板 属性 而应该是 html 属性 在 sendMail 对象中:

 let mailOptions = await transporter.sendMail({
    from: '"Test - No Reply" <test@test.com>'
    to: 'someEmail@gmail.com, 
    subject: "Hello ✔",
    html: './templates/welcome.hbs',
  });

回答我自己的问题。看起来这个问题与使它成为一个带有回调的异步函数有关,这导致了某种类型的未定义问题。

有关代码参考的深入答案,您可以在此处查看解决方案:https://github.com/yads/nodemailer-express-handlebars/issues/43

我的解决方案是:

const path = require('path')
const nodemailer = require('nodemailer');
const hbs = require('nodemailer-express-handlebars')

const { host, port, user, pass } = require('../config/mail.json')

var transport = nodemailer.createTransport({
    host,
    port,
    auth: { user, pass },
});

const handlebarOptions = {
    viewEngine: {
        extName: ".html",
        partialsDir: path.resolve('./src/resources/mail'),
        defaultLayout: false,
    },
    viewPath: path.resolve('./src/resources/mail'),
    extName: ".html",
};

transport.use('compile', hbs(handlebarOptions));

module.exports = transport;

我项目中的模板文件夹是:“src/resources/mail”

const mailer = require('../../modules/mailer');

mailer.sendMail({
            to: email,
            from: 'mail.example@examplemail.com',
            template: 'auth/forgot_password',
            context: { token },
        }, (err) => {
            if (err){
                console.log(err)
                return res.status(400).send({ error: 'Cannot send forgot password email'});
            }
            return res.send();
        })

我的模板文件是“auth/forgot_password.html”

在我的项目中,handlebarOptions 的配置出现问题,解决方案是:

const handlebarOptions = {
    viewEngine: {
        extName: ".html",
        partialsDir: path.resolve('./src/resources/mail'),
        defaultLayout: false,
    },
    viewPath: path.resolve('./src/resources/mail'),
    extName: ".html",
};