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

why template not working in reset password loopback

我只是搞不懂为什么模板在重置密码功能中不起作用

  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 的模板,那么我在电子邮件中收到空模板,例如下面的

    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 方法中使用相同的模板,那么为什么它在这里不起作用
是否有其他替代方法可以在密码重置

中提供模板

你好洛基你可以试试这样的东西

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);
             });

希望这对您和其他人也有帮助

我实际上是在使用 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);
       });