【发布时间】:2012-07-21 10:40:33
【问题描述】:
我正在经历一些错误场景,试图了解如何处理这些错误。
在没有数据库连接的情况下,Mongoose Model.find(...) 调用似乎挂起。在示例代码下方。我会假设回调是使用 err 对象调用的,但事实并非如此。
如何防止模型调用挂起?每次访问模型时是否必须手动检查readyState?
// app.js
// Let's use a non-existing host so connecting fails:
// (callback is invoked with err object)
mongoose.connect('mongodb://localhostXXX/blog', function(err){ ... });
BlogPost = mongoose.model('BlogPost', BlogPostSchema);
// api.js
exports.list_posts = function(req, res) {
// Ready state is '0' = disconnected (since we used a wrong hostname)
console.log('DB ready state: ' + BlogPost.db.readyState);
// This will not invoke the callback:
BlogPost.find(function(err, threads) {
// Never called...
});
}
【问题讨论】:
-
显示实现您在
BlogPost上调用的find方法的代码。 -
@ebohlman
BlogPost是猫鼬模型(我更新了代码)。