Loopbackjs 环回:重置后未调用事件resetPasswordRequest
我正在尝试在环回上点击密码重置过程,以便向用户发送一封包含说明的电子邮件。我创建了一个名为“user”的自定义模型,它扩展了“user”。然后我添加了“User.on('resetPasswordRequest'…”,但它从未执行过。请检查User.js上的console.log model-config.jsonLoopbackjs 环回:重置后未调用事件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.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
中隐藏内置的用户
模型?您可能会触发内置模型而不是扩展模型的密码重置事件。在用户
基本模型上已经有一个密码重置请求。应该足够使用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
}
},
....