Add nested remote method using loopbackjs

I use hardloop loopbackjs at work to implement the API.

For the model, CatI determined the remote method, called him meow.

So what can I do:

GET /cats/{:id}/meow

The model Catbelongs to the model User.

Now I would like to do something like this:

GET /users/{:id}/cats/{:id}/meow

Does anyone know how to do this?

I already tried nestRemoting, which only works for nested "drawing" methods.

+4
source share
3 answers

You can define the remote method in the user model and then use it to call the meow CatModel method

UserModel.someRemoteMethod = function(id1,id2,cb){
     CatModel.meow(id2,cb);
 }


  UserModel.remoteMethod(
    'someRemoteMethod',
    {
      accepts: [
        {arg: 'id1', type: 'number', required: true},
        {arg: 'id2', type: 'number', required: true}
      ],
      http: {path: '/:id1/cats/:id2/meow', verb: 'get'}
    }
  );
+2
source

nestRemoting('relationName'). , :

User.on('attached', function() {
   User.nestRemoting('catRelation');
}

user.js , , .

0

, .

nestRemoting json options, filterMethod, , , ​​(else if), (DoWhat )

server.models.Client.nestRemoting('units', {filterMethod: function(method, relation) {
        let regExp = /^__([^_]+)__([^_]+)$/;
        let matches = method.name.match(regExp);
        if (matches) {
            return '__' + matches[1] + '__' + relation.name + '__' + matches[2];
        } else if (method.name === 'DoWhat') {
            return method.name;
        }
    }});

,

0

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


All Articles