Search code examples
node.jsmongodbexpressmongoskin

MongoSkin don't work with req.params.id


I have this strange issue using mongskin for example

db.collection('collection_name')
.find({integer_id:req.params.id})
.ToArray(function(err,result){
console.log(result);
});

however the result will be empty array [ ] I tried to console.log(req.params.id) and the result is the integer number for example 1

if I use the number directly like

db.collection('collection_name')
.find({integer_id:1})
.ToArray(function(err,result){
console.log(result);
});

in this case the result will be fetched successfully


I don't know what is the issue exactly, is this is a character encoding issue? or something like that?

and please if there's any way to make sure that the req.params.id will work correctly with the database if there's any encoding issue

thank you in advanced


Solution

  • integer_id suggests that the id should be a Number, and not a String, which is what req.params.id will be.

    So try this:

    .find({ integer_id : Number(req.params.id) })