iplayjumanji2
iplayjumanji2

Reputation: 47

Adding parameter to express route, retrieves the opposite

I am passing a parameter for an api call to my express, but when I retrieve the json data it returns everything but what matches the query.

Here is the controller for finding the userid in my UserVote api:

exports.list = function(req, res){
  UserVote.find({userid: req.query.userid}, function(err, user_votes){
    if(err) { return handleError(res, err); }
    return res.json(200, user_votes);
  })
};

My route is as follows:

router.get('/ui/:userid', controller.list);

A sample of the json when I call the api without parameters /api/uservotes:

{"_id":"1","billid":"233","stance":"yea"},{"_id":"2","userid":"5524c71ba13d792907d47c14","billid":"234","stance":"yea"},{"_id":"3","userid":"5524","billid":"234","stance":"yea"},

and with parameters api/uservotes/ui/5524c71ba13d792907d47c14:

{"_id":"1","billid":"233","stance":"yea"}

Upvotes: 0

Views: 71

Answers (1)

Danny
Danny

Reputation: 354

You are trying to get the userid parameter the wrong way. You are defining the parameter as a route parameter but you are trying to access it as a query parameter. Instead of req.query.userid, you want to use req.params.userid, like this:

exports.list = function(req, res){
  UserVote.find({userid: req.params.userid}, function(err, user_votes){
    if(err) { return handleError(res, err); }
    return res.json(200, user_votes);
  })
};

Upvotes: 4

Related Questions