我有一个接受post
请求并将代码插入mongodb
数据库的代码。
app.post('/post_comment', function(req, res) {
Predicts.findOne({'id': parseInt(req.body.id)}, function(err, card) {
console.log('')
console.log(card)
console.log('')
if (card) {
Users.findOne( { 'userid': req.user ? req.user.userid : undefined }, function(err, user) {
console.log(user)
console.log('')
Predicts.update({'_id': card._id},
{$push : {comments: {login: user ? user.login : 'anonymous', content: '123'}}},
function(err, card1) {throw(err);
console.log('---')
console.log(card1);
})
})
}})
res.redirect('back')
})
此代码导致
node
进程完全冻结。仅重新启动node
进程可以提供帮助。调试显示,前四个
console.log
操作按预期方式工作,但console.log('---')
不会发生。这意味着Predicts.update
不起作用,但确实起作用,并且我可以在数据库中看到此请求的结果。什么是障碍?更新:我已将
Predicts.update
替换为Predicts.find
,但结果仍然相同。 Collback不起作用,并且node
进程冻结。Upd2:我确定
node
不会冻结,但只会将不需要的内容返回给mongodb
。 最佳答案
根据node-mongodb文档,update
函数如下所示:
collection.update(criteria, objNew, options, [callback]);
因此,如果您想使用回调,则应将其作为第4个参数,而将第3个作为您的选项(例如
multi:true
)或{}
。关于node.js - Node.js(Express)在mongodb操作中卡住,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22260531/