A2MetalCore
A2MetalCore

Reputation: 1639

How to Read Query Filters from Within a LoopBack Model

We're using LoopBack for our REST APIs and need to access the query filter (that was specified in the client) from within custom logic in the LoopBack model. For example, given this query:

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

how would we access the 'where' parameter from within the 'Menu.formatted' code:

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

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

Upvotes: 3

Views: 2588

Answers (2)

Jaime Franco
Jaime Franco

Reputation: 49

A way to introduce a filter should be something similar to this :

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 }
    });
};

In the example above, in the accepts field, which represents arguments that the remote method receives, you need to add the filter argument. This way you can use filter's query parameter value as an object.

Upvotes: 4

IvanZh
IvanZh

Reputation: 2290

Declare filter query paramerter as argument for your formatted remote method and then access it just like callback argument.

See how to describe arguments in docs.

Upvotes: 2

Related Questions