4

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

INFOSYS
  • 1,465
  • 9
  • 23
  • 50
  • https://loopback.io/doc/en/lb3/Remote-methods.html as you can read here the filter method isn't one of the properties of the remote method so you can include the filter in your path like `/getAllGlobalFilters?filter[something]=something&filter[limit]=3` – Anouar Kacem Nov 22 '17 at 10:44

1 Answers1

2

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!

Mark Ryan Orosa
  • 847
  • 1
  • 6
  • 21