【发布时间】:2014-01-24 19:41:06
【问题描述】:
在 MongoDB 中使用 Node/Express,我试图查找是否存在具有特定字段名称的对象。
我认为数据库连接本身没问题,因为我可以很好地将东西添加到数据库中(通过运行应用程序然后通过 mongo shell 检查来确认)
以下代码应该将 Class 的字段 student 增加 1,如果我遇到问题,则继续到下一页 /nextpage。
exports.join = function(req,res,next){
Class.find(({code:{$in: [req.body.roomNumber]}}), function(err, out){
if(err){res.redirect('/'); next(err);}
else{
console.log(out); // returns the whole object from db.
// for no match, I get "[]"
console.log(req.body.formEntry); // returns the entered form value OK
console.log(out.roomNumber); // always returns "undefined"; why?
if(out != null) { // always passes
console.log('adding 1 to students');
Class.update({code:req.body.formEntry}, {$inc:{'students':1}}); // also didn't work
res.redirect('/nextpage');
}else{
console.log('class not found');
}
}
};
}
我尝试过检查out.params.roomNumber(未定义)或out.next()(认为这是一个查询)之类的内容,但考虑到@ 987654329@ 给了我我想要的对象,它的所有字段都完好无损。我读了一个类似的问题,但问题在于异步性,因为整个事情都在回调中,我认为可能并非如此。
【问题讨论】:
-
您永远不会执行更新查询。
-
更多信息请见this answer。
-
啊,那是我必须忽略的另一个细节。谢谢。