节点js(Express)在mongodb操作中冻结
我有一个代码,接受一个post
请求,并插入一个string到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
进程才能提供帮助。 debugging显示,前四个console.log
操作按假定的方式工作,但console.log('---')
不会发生。 这意味着Predicts.update
不起作用,但它确实有效,我可以在数据库中看到这个请求的结果。 有什么困难?
Upd:我已经将Predicts.update
replace为Predicts.find
但结果仍然相同。 Collback不起作用, node
进程冻结。
Upd2:我确定node
不冻结,但只返回不需要mongodb
。
根据node-mongodb文档, update
函数如下所示:
collection.update(criteria, objNew, options, [callback]);
所以,如果你想使用callback,它应该是第四个参数,第三个应该是你的select(例如multi:true
),否则{}
。