我遇到了一个问题,我似乎无法解决过去几个小时。让我们开始吧。'null'进入Mongoose查询'findOne()'
目前使用的是最新快递的版本和猫鼬
我已经在我的route.js查询以下路线:
Note
.findOne({"user": req.body.user, "title": req.body.title, "subtitle":
req.body.subtitle})
.exec(function (err, note) {
if (err) {handleError(err)};
console.log(note._id);
})
//same implementation as the in the mongoose docs
然后,我有以下的客户端脚本Ajax请求:
let settings = {
type: 'GET',
url: 'http://localhost:8080/note',
data: {
"user": userId,
"title": noteTitle,
"subtitle": noteSubtitle
},
dataType: "json",
success: renderGetNoteTemplate
}
return $.ajax(settings);
现在的问题是,即使在一个文档中的数据库和右组确实存在,查询结果为空。在经过几个小时的阅读本网站的线程并记录到控制台之后,我决定将查询记录到控制台,并看到了这个!
_conditions: { subtitle: null, title: null, user: null }
所以传递的条件为空,这显然将返回null。现在,这是抓住。该查询与如条件下工作:
Note.findById(req.cookies.note).exec()
,我可以看到存在差异,该请求不是来自身体的到来。所以我上面通过Ajax请求发送的条件与类似
{"title": req.body.title}
最后,当我刚打印req.body.title等控制台没有它传递到查询,值来向上。例如,我将罗密欧与朱丽叶作为noteTitle传入,当我在服务器控制台上打印它时,我得到了Romeo And Juliet的价值。只要我将它传递给查询作为出现问题的条件。
字面上看起来无处不在,尤其是我给模型命名的方式等等。再次,当我将req.body参数传入条件时,我得到空结果。
此外,为了简洁起见,我没有在代码中包含bodyParser,所以当它没有进入查询条件时,它们会显示出来。
什么使这些参数变为空的任何想法?
感谢
谢谢,这就是问题所在。 – Bryan
不客气。 – luisenrike