Access methods of other models from a custom method in Loopback

I am trying to create a custom method for a custom model in Loopback.

The method calls the login, and then extracts the user role and adds it to the response, therefore, the token and role data are simultaneously stored in the login request.

My problem is that as soon as I get the token information, I don’t know how to call the Role and RoleMapping methods from what I create ...

How to add these models to the current area?

How can I access rootScope from this method?

Here is how I did it:

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'}
    }
  );
};
+4
source share
1 answer

You can return to appfrom your models, for example,TiUser.app

, , Model:

TiUser.app.models.Roles.find ..

+7

Source: https://habr.com/ru/post/1607629/


All Articles