【问题标题】:loopback - append filter before get requestloopback - 在获取请求之前附加过滤器
【发布时间】:2015-09-30 09:37:53
【问题描述】:

我正在将 Loopback 用于节点 JS 应用程序。 Loopback 自动生成 CRUD api。所以,我试图改变一个get 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();
});

这是更改请求 url 但不更改响应,它是相同的并且不包括表。我尝试更改 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 得到相同的结果。救命!!!

【问题讨论】:

    标签: javascript node.js express loopbackjs


    【解决方案1】:

    您可以通过添加“范围”部分将 ExpenseCategory 模型默认范围设置为包括供应商:

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

    【讨论】:

    • 这行得通,谢谢:) 但我需要在 req 中编辑查询,因为必须从会话中获取 user_id。并提供帮助。我试过beforeRemote()
    • 这取决于您想要它的用途 - 如果是用于身份验证,那么您是否看到过这个问题? afterRemote 可能是您需要的 - stackoverflow.com/questions/26661968/…
    【解决方案2】:

    一种解决方案是像这样覆盖find 远程路径:

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

    更新:这在以前的 Loopback 版本中有效,在当前版本中,您还必须在 expensecategories.setup() 调用之后执行此操作:

    expensecategories.disableRemoteMethod('find', true);
    

    Update2:这就是你应该如何设置你的模型:

    // This is automatically called by loopback
    MyModel.setup = function() {
      // Super setup
      MyModel.base.setup.apply(this, arguments);
    
     // Your customization
      MyModel.remoteMethod(...);
      MyModel.disableRemoteMethod(...);
    };
    

    【讨论】:

    • 这将在/expensecategories 创建一个新的api,而不是替换旧的。两个 api 在同一个端点。他们的反应都是一样的。这个我试过了。
    • 这很奇怪,因为我刚刚在一个项目上对其进行了测试并且它有效。
    • expensecategories.disableRemoteMethod('find', true); 这会阻止两个 api 调用。然后我在自定义 api 上收到 500 错误。我必须在哪里打电话给expensecategories.setup()。 ?
    • 我指的是this setup method。我不记得我在哪里看到的,但我总是把 remoteMethoddisableRemoteMethod 电话放在里面。基本设置是您的自定义模型将从其基本远程方法继承的位置。我用代码示例更新了我的答案。
    【解决方案3】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2014-10-27
      • 2013-06-05
      • 1970-01-01
      • 2020-12-27
      • 1970-01-01
      • 2015-11-21
      • 2019-07-23
      • 1970-01-01
      • 2016-08-15
      相关资源
      最近更新 更多