Error: A partials dir must be a string or config object in handlebars
Error: A partials dir must be a string or config object in handlebars
我正在使用 nodemailer
和 nodemailer-express-handlebars
发送模板化电子邮件,但是每当我尝试发送邮件时,我都会收到
Error: A partials dir must be a string or config object.
不知道是什么问题
const express = require('express');
const hbs = require('nodemailer-express-handlebars');
const nodemailer = require('nodemailer');
const app = express();
const user_name = 'xxxxxxxxxxx@gmail.com';
const refresh_token = 'xxxxxxxxxxxxxxxxxxxxxxxx';
const client_id = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
const client_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
const email_to = 'yyyyyyyyyyyyyyyyyy@gmail.com';
let transporter = nodemailer
.createTransport({
service: 'Gmail',
auth: {
type: 'OAuth2',
clientId: client_id,
clientSecret: client_secret
},
tls:{
rejectUnauthorized: false
}
});
transporter.use('compile', hbs({
viewPath: 'views/email',
extName: '.hbs'
}));
transporter.on('token', token => {
console.log('A new access token was generated');
console.log('User: %s', token.user);
console.log('Access Token: %s', token.accessToken);
console.log('Expires: %s', new Date(token.expires));
});
let mailOptions = {
from : user_name,
to : email_to,
subject : 'Hello ✔',
text : 'Hello world ?',
template: 'emailt',
context: {},
auth : {
user : user_name,
refreshToken : refresh_token,
expires : 1494388182480
}
};
// send mail with defined transport object
app.get('/', (req,res) => {
transporter.sendMail(mailOptions).then( r => {
res.send(r);
}).catch(e =>{
res.send(e);
});
});
app.listen(3000 ,()=>{
console.log('port: 3000');
});
我的目录如下所示views>email>emailt.hbs
看起来最近对 express-handlebars 的更改导致了这个问题,有人在此处发布了修复 https://github.com/yads/nodemailer-express-handlebars/issues/22
或
我猜你可以回到没有这个问题的版本,至少在它被修复之前。
我设法使我的代码正常工作,发布以防万一。我必须将 partialsDir
添加到我的 handlebarOptions
之前我没有该字段的地方:
const handlebarOptions = {
viewEngine: {
extName: '.hbs',
partialsDir: 'src/path',
layoutsDir: 'src/path',
defaultLayout: 'email.hbs',
},
viewPath: 'src/path',
extName: '.hbs',
};
transporter.use('compile', hbs(handlebarOptions));
希望对您有所帮助。
我正在使用 nodemailer
和 nodemailer-express-handlebars
发送模板化电子邮件,但是每当我尝试发送邮件时,我都会收到
Error: A partials dir must be a string or config object.
不知道是什么问题
const express = require('express');
const hbs = require('nodemailer-express-handlebars');
const nodemailer = require('nodemailer');
const app = express();
const user_name = 'xxxxxxxxxxx@gmail.com';
const refresh_token = 'xxxxxxxxxxxxxxxxxxxxxxxx';
const client_id = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
const client_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
const email_to = 'yyyyyyyyyyyyyyyyyy@gmail.com';
let transporter = nodemailer
.createTransport({
service: 'Gmail',
auth: {
type: 'OAuth2',
clientId: client_id,
clientSecret: client_secret
},
tls:{
rejectUnauthorized: false
}
});
transporter.use('compile', hbs({
viewPath: 'views/email',
extName: '.hbs'
}));
transporter.on('token', token => {
console.log('A new access token was generated');
console.log('User: %s', token.user);
console.log('Access Token: %s', token.accessToken);
console.log('Expires: %s', new Date(token.expires));
});
let mailOptions = {
from : user_name,
to : email_to,
subject : 'Hello ✔',
text : 'Hello world ?',
template: 'emailt',
context: {},
auth : {
user : user_name,
refreshToken : refresh_token,
expires : 1494388182480
}
};
// send mail with defined transport object
app.get('/', (req,res) => {
transporter.sendMail(mailOptions).then( r => {
res.send(r);
}).catch(e =>{
res.send(e);
});
});
app.listen(3000 ,()=>{
console.log('port: 3000');
});
我的目录如下所示views>email>emailt.hbs
看起来最近对 express-handlebars 的更改导致了这个问题,有人在此处发布了修复 https://github.com/yads/nodemailer-express-handlebars/issues/22
或
我猜你可以回到没有这个问题的版本,至少在它被修复之前。
我设法使我的代码正常工作,发布以防万一。我必须将 partialsDir
添加到我的 handlebarOptions
之前我没有该字段的地方:
const handlebarOptions = {
viewEngine: {
extName: '.hbs',
partialsDir: 'src/path',
layoutsDir: 'src/path',
defaultLayout: 'email.hbs',
},
viewPath: 'src/path',
extName: '.hbs',
};
transporter.use('compile', hbs(handlebarOptions));
希望对您有所帮助。