【问题标题】:Node + MongoDB: params not working inside find callNode + MongoDB:参数在 find 调用中不起作用
【发布时间】: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


【解决方案1】:

解决了。我只需要使用 parseInt 将参数转换为整数。

note_id: parseInt(request.params.note_id)

【讨论】:

    猜你喜欢
    • 2016-09-30
    • 2021-03-21
    • 2016-12-19
    • 2021-08-02
    • 1970-01-01
    • 2013-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多