title: Loopback. How to get all roles for one user?
body: <p>I'm creating a Loopback application and have created a custom user model, based on built-in User model.</p>
  
  <pre><code>{
    "name": "user",
    "base": "User",
    "idInjection": true,
    "properties": {
      "test": {
        "type": "string",
        "required": false
      }
    },
    "validations": [],
    "acls": [],
    "methods": []
  }
  </code></pre>
  
  <p>Then in boot script I'm creating (if not exists) new user, new role and a roleMapping.</p>
  
  <pre><code>User.create(
  { username: 'admin', email: 'admin@mail.com', password: 'pass' }
  , function (err, users) {
      if (err) throw err;
  
      console.log('Created user:', users);
  
      //create the admin role
      Role.create({
          name: 'admin'
      }, function (err, role) {
          if (err) throw err;
  
          //make user an admin
          role.principals.create({
              principalType: RoleMapping.USER,
              principalId: users.id
          }, function (err, principal) {
              if (err) throw err;
              console.log(principal);
          });
      });
  });
  </code></pre>
  
  <p>Then in custom remote method I'm trying to get all roles for User, using user's id. <a href="https://loopback.io/doc/en/lb3/HasMany-relations.html" rel="nofollow noreferrer">Loopbacks' documentation on this topic</a> says that </p>
  
  <blockquote>
    <p>Once you define a “hasMany” relation, LoopBack adds a method with the relation name to the declaring model class’s prototype automatically. For example: Customer.prototype.orders(...).</p>
  </blockquote>
  
  <p>And gives this example:</p>
  
  <blockquote>
    <p>customer.orders([filter],
      function(err, orders) {
      ...
    });</p>
  </blockquote>
  
  <p>But when I am trying to use <code>User.roles()</code> method, (const User = app.models.user;) I get the next error:</p>
  
  <blockquote>
    <p>TypeError: User.roles is not a function</p>
  </blockquote>
  
  <p>But when I'm making a remote request <code>http://localhost:9000/api/users/5aab95a03e96b62718940bc4/roles</code>, I get the desired roleMappings array.</p>
  
  <p>So, i would appreciate if someone could help get this data using js. I know I can probably just query the RoleMappings model, but I've wanted to do it the documentation-way.</p>