如何从LoopBack模型中读取查询过滤器

时间:2015-01-21 03:57:20

标签: model loopbackjs strongloop

我们在REST API中使用LoopBack,并且需要从LoopBack模型中的自定义逻辑中访问查询过滤器(在客户端中指定)。例如,给定此查询:

http://localhost:1337/api/Menus/formatted?filter[where][id]=42

我们如何访问'其中'来自' Menu.formatted'中的参数代码:

function asMenu(Menu) {
    Menu.formatted = function (callback) {

        <<Need to access the query filter here...>>

2 个答案:

答案 0 :(得分:4)

引入过滤器的方法应该与此类似:

module.exports = function(Menu) {
    Menu.formatted = function (filter,callback) {
      // Your code here
    }
    Menu.remoteMethod('formatted', {
      http: { path: '/formatted', verb: 'get' },
      accepts: [
        { arg: 'filter', type: 'object', 'http': { source: 'query' } }
      ],
      returns: { type: 'object', root: true }
    });
};

在上面的示例中,在accepts字段中,它表示远程方法接收的参数,您需要添加filter参数。这样,您可以将filter的查询参数值用作对象。

答案 1 :(得分:2)

filter查询参数声明为formatted远程方法的参数,然后像callback参数一样访问它。

了解如何describe arguments in docs