从Loopback中的自定义方法访问其他模型的方法

F.D*_*.F. 4 node.js loopbackjs

我正在尝试在Loopback中为基于用户的模型创建自定义方法.

该方法调用login,然后检索用户的角色并将其添加到响应中,以便登录请求立即保存令牌和角色信息.

我的问题是,一旦我有令牌信息,我不知道如何从我正在创建的那个中调用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'}
    }
  );
};
Run Code Online (Sandbox Code Playgroud)

con*_*adj 7

您可以app从模型中返回TiUser.app

所以我调用其他Model方法的方式是:

TiUser.app.models.Roles.find 等等