gpt4 book ai didi

node.js - Mongoose Model.find() 在未连接到数据库时挂起

转载 作者:IT老高 更新时间:2023-10-28 23:25:04 24 4
gpt4 key购买 nike

我正在经历一些错误场景,试图了解如何处理这些错误。

在没有数据库连接的情况下,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...
});
}

最佳答案

这不是一个答案,但希望它能帮助您找到解决方案。有非常相似的问题mongoose.createConnection在使用护照模块时,发现它可以正常工作mongoose.connect

关于node.js - Mongoose Model.find() 在未连接到数据库时挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11608397/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com