为什么模板在重置密码环回中不起作用

时间:2018-08-02 14:35:42

标签: node.js express loopbackjs

我只是混淆了为什么模板无法在重置密码功能中工作

  user.on('resetPasswordRequest', function(info) {
   var url = 'http://' + config.host + ':' + config.port + '/reset-password';
   var html = 'Click <a href="' + url + '?access_token=' +
    info.accessToken.id + '">here</a> to reset your password';
     user.app.models.Email.send({
          to: info.email,
          from: info.email,
          subject: 'Password reset',
          html: html
    }, function(err) {
       if (err) return console.log('> error sending password reset email');
      console.log('> sending password reset email to:', info.email);
   });
 });

上面的代码工作正常,但是如果我使用html的intead模板,那么我在电子邮件中会得到空模板,例如下面的

    user.app.models.Email.send({
          to: info.email,
          from: info.email,
          subject: 'Password reset',
         template: path.resolve(__dirname, '../../server/views/verify.ejs'),
    }, function(err) {
       if (err) return console.log('> error sending password reset email');
      console.log('> sending password reset email to:', info.email);
   });

如果我在user.verify方法中使用相同的模板,那么它在那里工作,那么为什么在这里不工作
还有其他方法可以在密码重置中提供模板

2 个答案:

答案 0 :(得分:1)

您好,您可以尝试使用

var ejs = require("ejs");

var token = info.accessToken.id;
var pathreset= path.resolve(__dirname, '../../server/views/resetpassword1.ejs');

ejs.renderFile(pathreset, { username: info.user.username, token:token }, function (err, data) {
        if (err) {
                console.log(err);
         } else {

             var options={
                to: info.email,
                from: credentials.user,
                subject: 'Password reset',
                html:data
                }

             User.app.models.Email.send(options, function(err) {
                    if (err) return console.log('> error sending password reset email');
                     console.log('> sending password reset email to:', info.email);
             });

希望这对您有帮助

答案 1 :(得分:1)

我实际上使用的是 html 而不是 template 属性,并创建具有环回的模板,因此您的代码如下所示:

 var loopback = require('loopback');
 var render = loopback.template(path.join(__dirname, '../../server/views/verify.ejs'));
 var html = render(
    // only if you need to map some fields in your .ejs file
    {
            firstName: firstname,
            lastName: lastname,
            link: confirmLink
    }
);

    user.app.models.Email.send({
              to: info.email,
              from: info.email,
              subject: 'Password reset',
              text: html,
              html: html
        }, function(err) {
           if (err) return console.log('> error sending password reset email');
          console.log('> sending password reset email to:', info.email);
       });