I am having an issue where nodemailer-express-handlebars is outputing the error:
[Error: ENOENT: no such file or directory, open ''] {
errno: -2,
code: 'ENOENT',
syscall: 'open',
path: '/Users/person/Code/app/api/controllers/templates/undefined.hbs'
My setup is as follows:
const handlebarOptions = {
viewEngine: {
extName: ".hbs",
partialsDir: path.resolve(__dirname, "templates"),
defaultLayout: false
},
viewPath: path.resolve(__dirname, "templates"),
extName: ".hbs"
};
transporter.use('compile', hbs(handlebarOptions));
and sending the email with:
let mailOptions = await transporter.sendMail({
from: '"Test - No Reply" <test@test.com>'
to: 'someEmail@gmail.com,
subject: "Hello ✔",
template: 'welcome',
});
Oddly enough I still receive the email even though it says it cant find the file. How can I resolve this error and have nodemailer-express-handlebars not see the files as 'undefined.hbs'?
UPDATE:
I changed 'welcome' to 'welcome.hbs' in the nodemailer and now the error is saying it can't find 'welcome.hbs.hbs'. This makes sense, you would think the solution would be to remove the '.hbs' and make it 'welcome' but then we are back to the original error of 'undefined.hbs'.
Also, if i change the template to 'welcome2' it says it cant find 'welcome2.hbs'. Its weird... it's as though it becomes undefined only when the template file matches the filename which is what it should be.
Answering my own question. It looks as though the issue had to do with making it a async function with a callback which caused some type of undefined issue.
For an in depth answer with code reference you can see the solution here: https://github.com/yads/nodemailer-express-handlebars/issues/43