0

I have a model, using postman I am sending request to this model, and in response I am getting the complete record(suppose 25 columns I have), so in response I am getting 25 columns back. I want to see only one column in the response, how can I change that. In javascript file, I have already written before save and after save methods.

Instead of all the columns detail, i want only one column detail in response which i m getting in postman

hakuna
  • 3
  • 3

1 Answers1

0

Solution 1:

For one time requests, you can pass on the filter query object in the get request with fields options specifying the columns you need.

Example: To only return the id, and name column.

{
  "fields": {
    "id": true,
    "name": true
  }
}

fields can also be supplied on server side like this:

yourRepository.find({fields: ['id', 'name']});

Docs: https://loopback.io/doc/en/lb4/Fields-filter.html

Solution 2:

To disable returning the columns in every request, specify hiddenProperties in model definition.

Like below:

@model({
  settings: {
    hiddenProperties: ['password'] // 
  }
})
class MyUserModel extends Entity {
  @property({id: true})
  id: number;
   @property({type: 'string'})
  email: string;
   @property({type: 'string'})
  password: string;
  ...
}

Properties specified in hiddenProperties will never be returned in responses, you can access them in your js/ts code if you need.

Docs: https://loopback.io/doc/en/lb4/Model.html#hidden-properties

Shubham P
  • 1,164
  • 4
  • 18
  • Thanks for the reply. One more concern i have. can i get the status code as the api response. like instead of all the fields, if i want only the status code whether it is passed or not. how can i achieve that. – hakuna Nov 17 '22 at 07:50
  • You can mark this answer as accepted if it solved your query. For your status codes query, it is advisable you read them on client side from headers. Feel free to post another question to discuss more options in order to keep this conversation relevant. – Shubham P Nov 17 '22 at 07:58