ema*_*rel 5 node.js express handlebars.js nodemailer express-handlebars
我遇到一个问题,nodemailer-express-handlebars 输出错误:
\n[Error: ENOENT: no such file or directory, open ''] {\n errno: -2,\n code: 'ENOENT',\n syscall: 'open',\n path: '/Users/person/Code/app/api/controllers/templates/undefined.hbs'\nRun Code Online (Sandbox Code Playgroud)\n我的设置如下:
\n const handlebarOptions = {\n viewEngine: {\n extName: ".hbs",\n partialsDir: path.resolve(__dirname, "templates"),\n defaultLayout: false\n },\n viewPath: path.resolve(__dirname, "templates"),\n extName: ".hbs"\n };\n\n transporter.use('compile', hbs(handlebarOptions));\nRun Code Online (Sandbox Code Playgroud)\n并发送电子邮件:
\n let mailOptions = await transporter.sendMail({\n from: '"Test - No Reply" <test@test.com>'\n to: 'someEmail@gmail.com, \n subject: "Hello \xe2\x9c\x94",\n template: 'welcome',\n });\nRun Code Online (Sandbox Code Playgroud)\n奇怪的是,我仍然收到电子邮件,尽管它说找不到该文件。如何解决此错误并使 nodemailer-express-handlebars 不会将文件视为“undefined.hbs”?
\n更新:
\n我在节点邮件程序中将“welcome”更改为“welcome.hbs”,现在错误提示找不到“welcome.hbs.hbs”。这是有道理的,您可能认为解决方案是删除“.hbs”并使其“欢迎”,但随后我们又回到了“undefined.hbs”的原始错误。
\n另外,如果我将模板更改为“welcome2”,它会说找不到“welcome2.hbs”。它很奇怪......就好像只有当模板文件与它应该是的文件名匹配时它才变得未定义。
\n小智 5
我的解决方案是:
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;
Run Code Online (Sandbox Code Playgroud)
我的项目中的模板文件夹是:“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();
})
Run Code Online (Sandbox Code Playgroud)
我的模板文件是“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",
};
Run Code Online (Sandbox Code Playgroud)
回答我自己的问题。看起来这个问题与使其成为带有回调的异步函数有关,这导致了某种类型的未定义问题。
有关代码参考的深入答案,您可以在此处查看解决方案: https: //github.com/yads/nodemailer-express-handlebars/issues/43
| 归档时间: |
|
| 查看次数: |
6317 次 |
| 最近记录: |