Search code examples
loopbackjsloopback

Loopback - Creating a get request with fields


Hello and thanks for taking your time to help me,

So I'm new to loopback, I'd like to create a request that retrieves all the data from a data source but only specifics fields. I've read all the tutorials on the loopback guide, but I don't understand how to proceed.

Basically what i have is that :

XXXX.getUserWithXXXX = function(cb) {
      cb(null, 'Greetings... ');
    }
XXXX.remoteMethod('getUserWithXXXX', {
      description: "Get all users who own a XXXX",
      returns: {arg: 'greeting', type: 'string'},
      fields: {id: true, email: true},
      http: {path: '/getUserWithXXXX', verb: 'get'}
    });

So first, what I want to create a request that will retrieve all the data from my model, so I could filter it And then I don't know how to filter in the code.

If anyone has any hints I'd gladly take them.


Solution

  • put the GET filters in "accept" attribute and also use "fields" filter to return specific fields of the documents.

    XXXX.getUserWithXXXX = function(id, email, cb) {
        app.models.XXXX.find({where:{id:"id", email:"email"}, fields:{specific_field1:1, specific_field2:1}}, function(err, returnedUsers){
            cb(err, returnedUsers)
        })
    }
    
    XXXX.remoteMethod('getUserWithXXXX', {
        description: "Get all users who own a XXXX",
        returns: {arg: 'greeting', type: 'string'},
        accepts: [{arg: "id",type:"string"}, {arg: "email", type:"string"}],
        http: {path: '/getUserWithXXXX', verb: 'get'}
    });