node.js - How to Filter by sportsname -
i developing app have filter sportsname. using node.js , mongodb in backend.
here code:-
router.route('/user/:_id/explore/:sportsname') .get(function(req, res, next){ var query = host.find(); if(req.query.sportsname){ query.where({sportsname:req.query.sportsname}); } query.exec(function(err,sportsname){ if(err) homecoming next(err); res.json(sportsname); console.log(sportsname); }) });
here document. want search users sportsname
[ { "_id":"5460e2af4ee5216f17000061", "maxplayer":"1", "minplayer":"11", "date":"10 august 2014", "venue":"google", "time":"afternoon", "sportsname":"lawn tennis", "__v":0 }, { "_id":"5460ff1b4ee5216f17000065", "maxplayer":"0", "minplayer":"9", "date":"10 oct 2014", "venue":"google", "time":"early morning", "sportsname":"cricket", "__v":0 }, { "_id":"5461a4014ee5216f17000089", "maxplayer":"4", "minplayer":"29", "date":"13 dec 2014", "venue":"google", "time":"afternoon", "sportsname":"lawn tennis", "__v":0 } ]
when running on time not filtering.
firstly, you're using req.query.sportsname
this getting parameter query string.
as others have mentioned in comments, should utilize req.params.sportsname
alternative utilize `req.param('sportsname')
this parameter in next order:
req.params req.body req.queryhowever, minor point
one issue see have spaces / capitalisation in sportsname
in
"sportsname":"lawn tennis",
but you're passing these in via url params - "/user/id/explore/lawn tennis" isn't valid valid url, , encoded "lawn%20tennis"
one way round utilize decodeuricomponent
-
so, seek changing query to:
query.where({sportsname:decodeuricomponent(req.params.sportsname)});
(note utilize of req.params
rather req.query
)
another way store additional element in document - sportsnameslug - create @ time of saving document (converting "lawn tennis" "lawn-tennis" example)
then querying on that
node.js mongodb mongoose mongodb-query
No comments:
Post a Comment