2012-03-19 62 views
0

我正在学习并尝试使用node.js和mongoskin的简单示例。这里是我的功能如下Node.js和mongoskin错误处理问题

以下函数的问题是,如果mongodb服务器断开,那么我也得到“err = null”,因此不能捕获连接错误。如果我重新启动的node.js服务器(而MongoDB的服务器仍在断开),我得到错误的 “[错误:无法连接到[服务器aa070:27017]”

// Process messages from client 
    app.post('/send', function(req, res){ 
    var message = { 
     id: i++, 
     nickname: req.param('nickname', 'Anonymous'), 
      text: req.param('text', ''), 
created_at: new Date() 
}; 

conn.chat_log.insert(message, function(err) { 
    if(err!==null){ 
    console.log(err); 
    } 
    else { 
    console.log(message); 
    console.log(err); 
    } 
}); 
res.json({status: 'ok'}); 
}); 
+1

确定蒙戈运行? – Swift 2012-04-09 01:53:31

回答

0

我是新来的节点.js和mongodb,但为什么使用if(err!==null)而不是if(err)?如果我的理解正确,难道这不能解决你的问题吗?

+0

其实,我想我误解了你的问题。抱歉。你是说当你无法连接到数据库,你没有得到一个值为“null”的错误? – Akrasiac 2012-04-09 02:33:15

+0

我的意思是我需要一些方法来了解mongoDB没有连接,只要它在node.js中断开连接。所以该错误对象应该返回错误,如“MongoDB未连接” – Swati 2012-04-26 11:20:43

0

不知道mongoskin,但对于节点蒙戈本机驱动程序(即mongoskin司机是建立在)对笔者说:

Note that there's no reason to pass a callback to the insert or update commands unless you use the safe:true option. If you don't specify safe:true, then your callback will be called immediately.