INFOSYS
INFOSYS

Reputation: 1585

Filter Remote Method in loopback

I have created a remote method in loopback

  XXX.remoteMethod('getAllGlobalFilters', {
    'description': 'List of all GlobalFilters',
    'http': {
      'path': '/getAllGlobalFilters',
      'verb': 'get'
    },
    'accepts': [
      {'arg': 'filter', 'type': 'object', 'http': {'source': 'query'}}
    ],
    'returns': {'arg': 'allGlobalFilters', 'type': 'array'}
  });


  XXX.getAllGlobalFilters = function(arg, callback) {
    console.log(arg);
    rtbSspPubFilter.find({
      where: {
        type: 15
      },
      include: [{relation: 'XXX'}]
    }, function(err, data) {
      if (err) {
        return callback(err);
      }
      return callback(null, data);
    });
  };
};

I am able to use find and the remote method is working fine but i am not able to use the regular expression like filtering in the remote method how do i access loop back default filters for the same.

enter image description here

I want to use the json like filters that is available for native loopback models. like the image above

Upvotes: 4

Views: 1438

Answers (1)

Mark Ryan Orosa
Mark Ryan Orosa

Reputation: 867

First of all, as good practice in your remote function, instead of using arg use the name you defined in your remote method, which is filter. That way, when you'd have more than one properties defined, it will be less confusing.

But, in your issue, you just have to pass a string, then parse it into a JSON object in your function, like the following:

   XXX.remoteMethod('getAllGlobalFilters',
         {
            'accepts': [{'arg': 'filter','type': 'string'}],
           'http': {
               'path': '/getAllGlobalFilters',
               'verb': 'get'
             },
            'returns': {'arg': 'allGlobalFilters', 'type': 'array'}
         });

   XXX.getAllGlobalFilters = function(filter, callback) {
       filter = JSON.parse(filter);
       rtbSspPubFilter.find(filter, function(err, data) {
          if (err) {
            return callback(err);
          }
          return callback(null, data);
        });
   };

Hope this helps!

Upvotes: 2

Related Questions