错误:局部目录必须是车把中的字符串或配置对象

时间:2019-03-05 19:51:39

标签: node.js express handlebars.js nodemailer

我正在使用nodemailernodemailer-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

2 个答案:

答案 0 :(得分:3)

好像最近对快速车把的更改引起了此问题,有人在这里https://github.com/yads/nodemailer-express-handlebars/issues/22

发布了修复程序

我想您可以回到没有此问题的版本,至少要等到它解决为止。

答案 1 :(得分:2)

我设法使我的代码正常工作,以防万一。我必须将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));

希望这会有所帮助。