简体   繁体   English

在环回中过滤远程方法

[英]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. 我能够使用find并且远程方法工作正常但我无法使用正则表达式,如远程方法中的过滤如何访问循环默认过滤器相同。

在此输入图像描述

I want to use the json like filters that is available for native loopback models. 我想使用可用于本机环回模型的类似json的过滤器。 like the image above 像上面的图像

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 . 首先,作为远程​​函数的良好实践,不使用arg ,而是使用您在远程方法中定义的名称,即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: 但是,在您的问题中,您只需传递一个字符串,然后将其解析为函数中的JSON对象,如下所示:

   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! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM