Sunday 21 May 2017

Sending only some data in a get request

so basically I want to only send some data back when I make a get request to my REST API. my code for the get request at the moment is :

`    router.get('/user/all', function(req, res, next){
      User.find({name: req.query.name}).then(function(assets){
        res.send(assets);
      });
});
`

Which returns:

`[
  {
    "_id": "546b454b5634563b546",
    "name": "John Doe",
    "email": "johndoe@johndoe.com",
    "phoneNo": "00000000000",
    "active": true,
    "__v": 0,
    "assets": [
      {
        "name": "house",
        "location": {
          "_id": "592190ce29f12e179446d837",
          "coordinates": [
            -81.5,
            24.1
          ],
          "type": "point"
        },
        "_id": "592190ce29f12e179446d836"
      }
    ]
  }
]`

But I want it to return only:

` [
      {
        "name": "house",
        "location": {
          "_id": "592190ce29f12e179446d837",
          "coordinates": [
            -81.5,
            24.1
          ],
          "type": "point"
        },
        "_id": "592190ce29f12e179446d836"
      }
    ]`

How can I change the API request to achieve this? Thanks, Ed.



via Ed Lynch

No comments:

Post a Comment