findById returning a list of documents instead of a single result findById returning a list of documents instead of a single result mongoose mongoose

findById returning a list of documents instead of a single result


The code is correct, the way you are making request is wrong

Your Format :

http://localhost:3000/events?id=someid

id = someID in the URL represents a GET query parameter.

If you are using wildcard parameters like :id in the route, then the request URL should be :

http://localhost:3000/events/someid

someid will now be present in req.params.id

You are getting all the objects is because the variable id is undefined in your case, undefined parameters are omitted by default. So it is same as calling : EventModel.findById({}, callback)

Thanks


You can try calling Model.findOne() instead of Model.findById

EventModel.findOne({id}, callback)