部署

时间:2018-06-14 23:17:55

标签: firebase handlebars.js nodemailer

根据车把网站和自定义助手示例的指导原则,我创建了以下Nodejs代码来运行Firebase项目的服务器端。

var nodemailer = require('nodemailer');
var hbs = require('nodemailer-express-handlebars');
var exphbs = require('express-handlebars')
var Handlebars = require('handlebars');

Handlebars.registerHelper('ifCond', function (v1, operator, v2, options) {

    switch (operator) {
        case '==':
             return (v1 == v2) ? options.fn(this) : options.inverse(this);
        case '===':
            return (v1 === v2) ? options.fn(this) : options.inverse(this);
        case '!=':
            return (v1 != v2) ? options.fn(this) : options.inverse(this);
        case '!==':
            return (v1 !== v2) ? options.fn(this) : options.inverse(this);
        case '<':
            return (v1 < v2) ? options.fn(this) : options.inverse(this);
        case '<=':
            return (v1 <= v2) ? options.fn(this) : options.inverse(this);
        case '>':
            return (v1 > v2) ? options.fn(this) : options.inverse(this);
        case '>=':
            return (v1 >= v2) ? options.fn(this) : options.inverse(this);
        case '&&':
            return (v1 && v2) ? options.fn(this) : options.inverse(this);
        case '||':
            return (v1 || v2) ? options.fn(this) : options.inverse(this);
        default:
            return options.inverse(this);
    }

});
var transporter = nodemailer.createTransport({
    service: 'gmail',
    tls:{
        rejectUnauthorized: false
    },
    auth: {
        user: automaticEmail.user,
        pass: automaticEmail.pass
    }
});

transporter.use('compile', hbs({
    viewPath: 'emails',
    extName: '.hbs'
}))

var mailOptions = {
    from: 'roteiroapp.suporte@gmail.com',
    to: user.email,
    subject: subject,
    template: 'email',
    context: {
        route: route,
        user: user,
    }
};

transporter.sendMail(mailOptions, function(error, info){
    if (error) {
        console.log(error);
    } else {
        console.log('Email sent: ' + info.response);
    }
});

所有这些代码都在index.js中,具有以下文件夹结构:

-functions

----- index.js

-----电子邮件

--------- email.hbs

测试本地服务器中的代码,一切正常,我收到收件箱中的电子邮件,控制台记录以下消息:

  

已发送电子邮件:250 2.0.0确定1528989119 y33-v6sm2821692ita.24 - gsmtp

部署项目时会出现问题,我从网上触发发送电子邮件功能。 firebase控制台记录以下错误:

  

{       错误:缺少助手:“ifCond”        at Object。(/ user_code/node_modules/nodemailer-express-handlebars/node_modules/handlebars/dist/cjs/handlebars/helpers/helper-missing.js:19:13)       在Object.eval(eval at createFunctionContext(/user_code/node_modules/nodemailer-express-handlebars/node_modules/handlebars/dist/cjs/handlebars/compiler/javascript-compiler.js:254:23),   :10:74)       在main(/user_code/node_modules/nodemailer-express-handlebars/node_modules/handlebars/dist/cjs/handlebars/runtime.js:175:32)       at ret(/user_code/node_modules/nodemailer-express-handlebars/node_modules/handlebars/dist/cjs/handlebars/runtime.js:178:12)       at ret(/user_code/node_modules/nodemailer-express-handlebars/node_modules/handlebars/dist/cjs/handlebars/compiler/compiler.js:526:21)       在ExpressHandlebars._renderTemplate(/user_code/node_modules/nodemailer-express-handlebars/node_modules/express-handlebars/lib/express-handlebars.js:247:12)       在ExpressHandlebars。 (/user_code/node_modules/nodemailer-express-handlebars/node_modules/express-handlebars/lib/express-handlebars.js:173:21)        description: undefined,        fileName:未定义,        lineNumber:未定义,       消息:'失踪帮手:“ifCond”',       名称:'错误',       数字: undefined}

有没有人知道如何正确注册ifCond助手,以便我可以在部署的项目中的email.hbs模板中使用它?

0 个答案:

没有答案