node.js 在连接到 mongodb atlas 时挂起
我在连接到本地 MongoDB 时遇到问题,所以我想尝试连接到 atlas 数据库。仍然没有运气。我认为问题出在此时我的 Windows 安装上。
我可以使用相同的 uri 毫无问题地使用 MongoDB Compass 进行连接。
不会抛出任何错误。只是挂起。
const MongoClient = require('mongodb').MongoClient;
const uri = "mongodb+srv://user:[email protected]/?retryWrites=true&w=majority";
MongoClient.connect(uri, function(err, db) {
if (err) throw err;
console.log('Connected to database!');
// Perform database operations here...
db.close();
});
如果您想了解我一直在处理的事情,请查看我以前的帖子。
我尝试禁用我的防火墙和 Windows Defender。 我确保用户名和密码周围没有< >。 我也试过猫鼬。
回答如下:问题是,来自 npm 的较新的 mongodb 包不再执行/调用提供的回调。
MongoClient.connect(...)
而是返回一个承诺。
这是一个突破性的变化。