2015-10-04 161 views
1

我使用以下连接到我的MongoDB。我对密码进行了拼写错误,认证失败,但是直到我进行了模型搜索而没有发现错误时,它才挂起。Nodejs - Moongose发现不会抛出错误,如果连接中断

如果没有开放的连接不应该模型抛出的错误,而不是挂?

mongoose.connect(uristring, function (err, res) { 
    if (err) { 
    console.log ('ERROR connecting to: ' + uristring + '. ' + err); 
    } else { 
    console.log ('Succeeded connected to: ' + uristring); 
    } 
}); 

User.findOne({ 
    'valid.email': Email 
    }, function(err, user) { 
    if (err) { 
     callback({ this is never called }); 
    } 

回答

1

尝试使用断开事件处理程序。

disconnected: Emitted after getting disconnected from the db.

conn.on('disconnected', callback); 
+0

我看不出如何能帮助。我想你可以尝试重新连接,但仍然不能解释为什么猫鼬只是挂起 – Drakoumel

+0

是的,你可以尝试重新连接或抛出一个错误。这是如何设计猫鼬的,它会在连接建立之前缓存查询。请参阅:https://github.com/Automattic/mongoose/issues/951 – Sohail

+0

嗯,我想那么这是“预期的行为”thnx! – Drakoumel