【发布时间】:2013-01-13 22:14:59
【问题描述】:
我在 MongoDB find 调用中遇到了参数问题。如果我通过 request.params.note_id 我得到未定义。如果我用数字值调用 find 我会得到一组正确的数据。
使用 request.params.note_id 调用。这只会返回一个空白视图。 Console.log 正确打印 id。
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
});
});
});
使用固定 id 2 调用。这将返回正确呈现的视图,其中包含 note_id 2 的内容。Console.log 正确记录 url 中的任何内容(比如“/show/22”打印 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
});
});
});
【问题讨论】:
-
我猜你需要将 note_id 转换为数字
-
那行得通。多么简单。谢谢!
标签: javascript node.js mongodb