【发布时间】:2017-03-25 15:33:42
【问题描述】:
我正在尝试利用 Loopback 上的密码重置过程,因此我可以向用户发送一封包含说明的电子邮件。我创建了一个名为“用户”的自定义模型,它扩展了“用户”。然后我添加了“User.on('resetPasswordRequest'...”,但它永远不会被执行。请检查user.js上的console.log
model-config.json
{
...
"user": {
"dataSource": "mysqlDs",
"public": true,
"options": {
"emailVerificationRequired": false
}
},
...
}
user.json
{
"name": "user",
"base": "User",
"idInjection": true,
"properties": {},
"validations": [],
"relations": {},
"acls": [
{
"principalType": "ROLE",
"principalId": "$everyone",
"accessType": "READ",
"permission": "ALLOW"
}
],
"methods": []
}
user.js
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');
});
});
};
【问题讨论】:
-
对我来说看起来不错。如何触发事件?你检查了 LoopBack user management example project 吗?重置密码流程是其中的一部分。
-
嗨@IvanSchwarz,感谢您的帮助。这正是我最初使用的演示。我正在触发 Loopback 创建的 API 面板上的事件。 user.js 文件由 Loopback 执行,因为我可以看到第一个 console.log 在终端上打印。但是 User.on 中的 console.log 永远不会被打印出来。
-
我刚刚克隆了loopback-getting-started repo,粘贴到你的
user文件中,调整了model-config.json和console.log()都打印了。我在想……你不是忘了把内置的User模型隐藏在model-config.json中吗?您可能会触发内置模型而不是扩展模型的密码重置事件。
标签: loopbackjs strongloop