从 Loopback 中的自定义方法访问其他模型的方法
Accessing other models' methods from custom method in Loopback
我正在尝试为 Loopback 中基于用户的模型创建自定义方法。
该方法调用登录,然后检索用户的角色并将其添加到响应中,以便登录请求立即包含令牌和角色信息。
我的问题是,一旦我获得了令牌信息,我就不知道如何从我正在创建的对象中调用 Role 和 RoleMapping 方法...
如何将这些模型添加到当前范围?
如何通过此方法访问 rootScope?
我是这样做的:
module.exports = function(TiUser) {
TiUser.auth = function(credentials, include, fn) {
var self = this;
self.login(credentials, include, function(err, token) {
var role = // Here I would retrieve Role related info
authInfo = {
token: token,
role: role
};
fn(err, authInfo);
});
};
TiUser.remoteMethod(
'auth',
{
description: 'Login method with Role data information embedded in return',
accepts: [
{arg: 'credentials', type: 'object', required: true, http: {source: 'body'}},
{arg: 'include', type: ['string'], http: {source: 'query' },
description: 'Related objects to include in the response. ' +
'See the description of return value for more details.'}
],
returns: {
arg: 'accessToken', type: 'object', root: true,
description: 'User Model'
},
http: {verb: 'post'}
}
);
};
您可以像这样从您的模型返回 app
TiUser.app
所以我调用其他模型方法的方式是:
TiUser.app.models.Roles.find
等
我正在尝试为 Loopback 中基于用户的模型创建自定义方法。
该方法调用登录,然后检索用户的角色并将其添加到响应中,以便登录请求立即包含令牌和角色信息。
我的问题是,一旦我获得了令牌信息,我就不知道如何从我正在创建的对象中调用 Role 和 RoleMapping 方法...
如何将这些模型添加到当前范围?
如何通过此方法访问 rootScope?
我是这样做的:
module.exports = function(TiUser) {
TiUser.auth = function(credentials, include, fn) {
var self = this;
self.login(credentials, include, function(err, token) {
var role = // Here I would retrieve Role related info
authInfo = {
token: token,
role: role
};
fn(err, authInfo);
});
};
TiUser.remoteMethod(
'auth',
{
description: 'Login method with Role data information embedded in return',
accepts: [
{arg: 'credentials', type: 'object', required: true, http: {source: 'body'}},
{arg: 'include', type: ['string'], http: {source: 'query' },
description: 'Related objects to include in the response. ' +
'See the description of return value for more details.'}
],
returns: {
arg: 'accessToken', type: 'object', root: true,
description: 'User Model'
},
http: {verb: 'post'}
}
);
};
您可以像这样从您的模型返回 app
TiUser.app
所以我调用其他模型方法的方式是:
TiUser.app.models.Roles.find
等