Loopbackjs 环回:重置后未调用事件resetPasswordRequest

Loopbackjs 环回:重置后未调用事件resetPasswordRequest,loopbackjs,strongloop,Loopbackjs,Strongloop,我正在尝试在环回上点击密码重置过程,以便向用户发送一封包含说明的电子邮件。我创建了一个名为“user”的自定义模型,它扩展了“user”。然后我添加了“User.on('resetPasswordRequest'…”,但它从未执行过。请检查User.js上的console.log model-config.json user.json user.js module.exports=函数(用户){ log('它在这里打印此日志'); User.on('resetPasswordRequest'

我正在尝试在环回上点击密码重置过程,以便向用户发送一封包含说明的电子邮件。我创建了一个名为“user”的自定义模型,它扩展了“user”。然后我添加了“User.on('resetPasswordRequest'…”,但它从未执行过。请检查User.js上的console.log

model-config.json
user.json
user.js
module.exports=函数(用户){
log('它在这里打印此日志');
User.on('resetPasswordRequest',函数(info){
log('但它从未在此处打印此日志');
var url='1〕http://www.example.com/reset-password';
var html='单击';
loopback.Email.send({
致:info.email,
发件人:'mail@example.com',
主题:“我的主题”,
html:html
},函数(){
console.log('>将密码重置电子邮件发送到:',info.email);
if(err)返回console.log(“>发送密码重置电子邮件时出错”);
});
});
};

难道你没有定义一个远程方法来调用你的代码吗? 这就是我要做的:

user.js

    module.exports = function(User) {

      console.log('It prints this log here.');

      User.resetPasswordRequest = function (info, cb) {

        console.log('But it does not print this log here ever.');

        var url = 'http://www.example.com/reset-password';
        var html = 'Click <a href="' + url + '?access_token=' + info.accessToken + '">here</a>';
        loopback.Email.send({
          to: info.email,
          from: 'mail@example.com',
          subject: 'My Subject',
          html: html
        }, function() {
          console.log('> sending password reset email to:', info.email);
          if (err) {
             cb(err, false);
             return console.log('> error sending password reset email');
          }
          cb(err, true):
        });
      });

    User.remoteMethod('resetPasswordRequest', {
            description: 'Send a mail to an User to permit him to reset his password',
            accepts: {arg: 'info', type: 'object', required: true},
            returns: {arg: 'mailSend', type: 'boolean'},
            http: {verb: 'post'}
        });

};
不要忘了在user.json中更新ACL,以便从远程调用该方法:

{
  "name": "user",
  "base": "User",
  "idInjection": true,
  "properties": {},
  "validations": [],
  "relations": {},
  "acls": [
    {
      "accessType": "*",
      "principalType": "ROLE",
      "principalId": "$everyone",
      "permission": "DENY"
    },
    {
      "accessType": "READ",
      "principalType": "ROLE",
      "principalId": "$everyone",
      "permission": "ALLOW"
    },
    {
      "accessType": "WRITE",
      "principalType": "ROLE",
      "principalId": "$authenticated",
      "permission": "ALLOW"
    },
    {
      "principalType": "ROLE",
      "principalId": "$everyone",
      "permission": "ALLOW",
      "property": "resetPasswordRequest"
    }
  ],
  "methods": []
}

解决方案如下所述:

因此,您需要:

User.setup = function() {
  var User = this;
  // since setup is called for every extended model
  // the extended model will also have the event listener
  User.on('resetPasswordRequest', function() {
    ///Do your stuff here
  });
}

好的,@IvanSschwarz是对的。非常感谢!我的初始代码是正确的,但它缺少model-config.json上的一个小更改:出于某种原因,我需要隐藏用户模型。因此,我也借此机会将我的模型名从“User”更改为“member”,以遵循环回良好实践指南

   ...
   "User": {
     "dataSource": "mysqlDs",
     "public": false
   },
   "member": {
     "dataSource": "mysqlDs",
     "public": true,
     "options": {
       "emailVerificationRequired": true
     }
   },
   ....

我相信其他建议也可能有效,但我想通过JSON扩展用户模型,并利用用户模型中内置的远程方法重置密码。所以,谢谢大家!

我觉得这很好。你如何触发事件?你检查环回了吗?重置密码流是其中的一部分。嗨@IvanSchwarz,谢谢你的帮助。这正是我第一次使用的演示。我正在触发Loopback创建的API面板上的事件。user.js文件由Loopback执行,因为我可以看到终端上打印的第一个console.log。但是user.on中的console.log从未打印。我刚刚克隆了repo,粘贴到你的
user
文件,调整了
model config.json
console.log()
已打印。我想知道……您是否忘了在
model config.json
中隐藏内置的
用户
模型?您可能会触发内置模型而不是扩展模型的密码重置事件。在
用户
基本模型上已经有一个密码重置请求。应该足够使用e> POST/users/resetendpoint。谢谢您F3L1X79,但我同意@IvanSchwarz。另外,我相信您的建议中没有发送正确的密码重置令牌。但无论如何,我尝试实现它,我得到了:request POST/api/users/resetPasswordRequest的未处理错误:错误:共享类“User”没有处理POST/resetPasswordRequest的方法可能是因为名称“resetPasswordRequest”是受保护的。我不知道这是一个内置方法。您可能想将名称更改为另一个。这看起来是理想的解决方案。但是,该代码从未执行。当我将console.log放在User.setup之前时,它将打印内容。User.setup中的console.log从未打印。只是出于好奇,完整的User.j您的建议是什么?您好@GeoffreyMureithi,我需要设置如下内容:var User=Model.extends('User');?如果需要,模型是什么?嗯,类似于
var User=Model.extends('User');
是必需的模型应该来自loopback对象
loopback.Model
,以及(我认为)如果有人想查看最终代码作为参考,请访问
User.app.Model
,如下所示:
{
 accessToken: xxx,
 email: xxx
}
{
  "name": "user",
  "base": "User",
  "idInjection": true,
  "properties": {},
  "validations": [],
  "relations": {},
  "acls": [
    {
      "accessType": "*",
      "principalType": "ROLE",
      "principalId": "$everyone",
      "permission": "DENY"
    },
    {
      "accessType": "READ",
      "principalType": "ROLE",
      "principalId": "$everyone",
      "permission": "ALLOW"
    },
    {
      "accessType": "WRITE",
      "principalType": "ROLE",
      "principalId": "$authenticated",
      "permission": "ALLOW"
    },
    {
      "principalType": "ROLE",
      "principalId": "$everyone",
      "permission": "ALLOW",
      "property": "resetPasswordRequest"
    }
  ],
  "methods": []
}
User.setup = function() {
  var User = this;
  // since setup is called for every extended model
  // the extended model will also have the event listener
  User.on('resetPasswordRequest', function() {
    ///Do your stuff here
  });
}
   ...
   "User": {
     "dataSource": "mysqlDs",
     "public": false
   },
   "member": {
     "dataSource": "mysqlDs",
     "public": true,
     "options": {
       "emailVerificationRequired": true
     }
   },
   ....