I'm having trouble with params inside a MongoDB find call. If I pass the request.params.note_id I get undefined. If I call find with a number value I get a correct set of data.
Call with request.params.note_id. This returns just a blank view. Console.log prints the id correctly.
app.get("/show/:note_id", function(request, response) {
console.log(request.params.note_id);
return db.notes.find({
note_id: request.params.note_id
}, function(err, notes) {
return response.render(__dirname + "/views/index.ejs", {
notes: notes
});
});
});
Call with a fixed id of 2. This returns a correctly rendered view with the contents of note_id 2. Console.log logs correctly whatever is in the url (say "/show/22" prints 22).
app.get("/show/:note_id", function(request, response) {
console.log(request.params.note_id);
return db.notes.find({
note_id: 2
}, function(err, notes) {
return response.render(__dirname + "/views/index.ejs", {
notes: notes
});
});
});
Solved. I just had to cast the params to an integer with parseInt.
note_id: parseInt(request.params.note_id)