loopback - 在获取请求之前附加过滤器

时间:2015-09-30 09:37:53

标签: javascript node.js express loopbackjs

我正在为节点JS应用程序使用Loopback。 Loopback auto生成CRUD api。所以,我试图改变一个获取api所以也包括一个表。我可以通过在查询中添加包含过滤器来实现此目的

/api/expensecategories?filter[include]=vendors

但我希望/api/expensecategories与表一起返回。为此,我使用beforeRemote()方法更改请求。

我的代码是:

expensecategories.beforeRemote('find', function (ctx, inst, next) {
   console.log(ctx.req.url);
   ctx.req.url = "/?filter[include]=vendors";
   console.log(ctx.req.url);
   console.log('GET api called');
   next();
});

这是更改请求网址但不更改响应,它是相同的,并且不包括表。我尝试更改req中的所有内容,如

ctx.req.query = {filter: { include: 'vendors' }};

但没用。任何想法我怎么能实现这一目标。两个表关系已经定义,我可以通过制作自定义api来获得所需的结果,

expensecategories.expensecategory = function (cb) {
    expensecategories.find({
      include: {
        relation : 'vendors',
      }
    }, function(err, data) {
    cb(null, data);
    });
  };

  expensecategories.remoteMethod (
    'expensecategory', {
       description: 'get all expense types + vendors',
       http: {path: '/yes', verb: 'get'},
       returns: {arg: 'expensecategory', type: 'string'}
    }
  );

所以,我的表关系和一切都是正确的。我也希望/expensecategories获得相同的结果。帮助!

3 个答案:

答案 0 :(得分:0)

一种解决方案是覆盖 <table class="responsive tableContentTable " width="100%" border="0" cellspacing="0" cellpadding="10"> <thead> <tr> <td class="fixedTableRow" data-offset-top="98" data-spy="affix"> <ul class="col-xs-12 col-sm-12 col-md-12 col-lg-12 nav"> <li> <img src="images/product1.jpg" height="" width="" /> </li> <li> <p>15 INCH MACBOOK PRO WITH RETINA DISPLAYMGXA2LL/A.</p> </li> </ul> </td> <td class="fixedTableRow2" data-offset-top="98" data-spy="affix"> <ul class="col-xs-12 col-sm-12 col-md-12 col-lg-12 nav"> <li> <img src="images/product1.jpg" height="" width="" /> </li> <li> <p>15 INCH MACBOOK PRO WITH RETINA DISPLAYMGXA2LL/A.</p> </li> </ul> </td> </tr> </thead> <tbody> <tr id="feature2" class="featureHeadingRow"> <td colspan="3" class="featureHeading"> Feature Group 2</td> </tr> <tr class="FeatureHeadRow"> <th valign="top" colspan="2"> Price </th> </tr> <tr> <td> <label class="spec_content">$999</label> </td> <td> <label class="spec_content">$999</label> </td> </tr> <tr class="FeatureHeadRow"> <th valign="top" colspan="2"> Processor Type </th> </tr> <tr> <td> <label class="">Intel core i5</label> </td> <td> <label class="">Intel core i5</label> </td> </tr> <tr class="FeatureHeadRow"> <th valign="top" colspan="2"> Hard Drive </th> </tr> <tr> <td> <label class="">500GB</label> </th> <td> <label class="">500GB</label> </td> </tr> <tr class="FeatureHeadRow"> <th valign="top" colspan="2"> MSRP </th> </tr> <tr> <td> <label class="">$999</label> </td> <td> <label class="">$999</label> </td> </tr> <tr class="FeatureHeadRow"> <th valign="top" colspan="2"> Release Year </th> </tr> <tr> <td> <label class="">2015</label> </td> <td> <label class="">2015</label> </td> </tr> <tr class="FeatureHeadRow"> <th valign="top" colspan="2"> OS Version </th> </tr> <tr> <td> <label class="">OS X 10.10 Yosemite</label> </td> <td> <label class="">OS X 10.10 Yosemite</label> </td> </tr> <tr class="FeatureHeadRow"> <th valign="top" colspan="2"> Display Technology </th> </tr> <tr> <td> <label class="">LED</label> </td> <td> <label class="">LED</label> </td> </tr> <tr class="FeatureHeadRow"> <th valign="top" colspan="2"> Resolution </th> </tr> <tr> <td> <label class="">2880*1800</label> </td> <td> <label class="">2880*1800</label> </td> </tr> </tbody> </table> 远程路径,如下所示:

find

更新:这在以前的Loopback版本中有效,在expensecategories.remoteMethod ( 'expensecategory', { description: 'get all expense types + vendors', http: { path: '/', // <-- HERE verb: 'get' }, returns: {arg: 'expensecategory', type: 'string'} } ); 调用后,您还必须执行此操作:

expensecategories.setup()

Update2 :您应该如何设置模型:

expensecategories.disableRemoteMethod('find', true);

答案 1 :(得分:0)

您可以通过添加&#34;范围&#34;将ExpenseCategory模型默认范围设置为包括供应商。部分:

"scope": {
    "include": "vendor"
  }

答案 2 :(得分:0)

我知道这个问题已经得到解答,但是我从Google到达这里遇到了类似的问题,因为我想在查询所有请求时添加另一个过滤器参数。

关系不对我有用,所以我继续挖掘并在文档中找到snippet on access hooks。所以最后我做了这个(基于你的例子):

expensecategories.observe('access', function (context, next) {
  context.query.where = context.query.where || {};
  context.query.where.include = 'vendors';
  next();
});