我正在尝试使用Loopback上的密码重置过程,因此我可以向用户发送带有说明的电子邮件。我创建了一个名为“user”的自定义模型,扩展了“User”。然后我添加了“User.on('resetPasswordRequest'...”,但它永远不会被执行。请检查user.js上的console.log
{
...
"user": {
"dataSource": "mysqlDs",
"public": true,
"options": {
"emailVerificationRequired": false
}
},
...
}
{
"name": "user",
"base": "User",
"idInjection": true,
"properties": {},
"validations": [],
"relations": {},
"acls": [
{
"principalType": "ROLE",
"principalId": "$everyone",
"accessType": "READ",
"permission": "ALLOW"
}
],
"methods": []
}
module.exports = function(User) {
console.log('It prints this log here.');
User.on('resetPasswordRequest', function(info) {
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.id + '">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) return console.log('> error sending password reset email');
});
});
};
答案 0 :(得分:3)
好的,所以@IvanSschwarz是对的。非常感谢!我的初始代码是正确的,但它缺少对model-config.json的一个小改动:我需要隐藏User模型由于某种原因。所以我也借此机会将我的模型名称从“user”更改为“member”,以遵循环回良好实践指南。
$scope.list().then(ap_list => console.log(ap_list));
我相信其他建议也可能有效,但我想通过JSON扩展User模型,并且还利用Users模型中的内置远程方法Reset Password。谢谢你们!
答案 1 :(得分:0)
不要错过定义远程方法来调用代码吗? 这就是我要做的事情:
<强> 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'}
});
};
然后,使用资源管理器或通过REST调用API,同时发布您的 info json,如:
{
accessToken: xxx,
email: xxx
}
并且不要忘记在 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": []
}
答案 2 :(得分:0)
这里解释了解决方案: http://loopback.io/doc/en/lb3/Extending-built-in-models.html#setting-up-a-custom-model
所以你需要:
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
});
}