英文:
node.js hangs when connecting to mongodb atlas
问题
I have been having trouble connecting to my local MongoDB, so I figured trying to connect to an atlas database. Still have no luck. I think the issue is with my windows install at this point.
我一直在尝试连接本地的 MongoDB,但还是无法成功。我认为问题可能出在我的 Windows 安装上。
I can connect using MongoDB Compass no problem, using the same uri.
我可以使用 MongoDB Compass 连接,使用相同的 URI,没有问题。
No errors are thrown. Just hangs.
没有错误抛出,只是卡住了。
const MongoClient = require('mongodb').MongoClient;
const uri = "mongodb+srv://user:password@main.9nwa88e.mongodb.net/?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();
});
Check my previous posts if you want an idea of what I've been dealing with.
如果您想了解我之前遇到的问题,可以查看我的先前帖子。
I tried disabling my firewall and windows defender.
我尝试禁用了防火墙和 Windows Defender。
I made sure not to have < > around the username and password.
我确保用户名和密码周围没有 < > 符号。
I also tried mongoose.
我还尝试了 Mongoose。
英文:
I have been having trouble connecting to my local MongoDB, so I figured trying to connect to an atlas database. Still have no luck. I think the issue is with my windows install at this point.
I can connect using MongoDB Compass no problem, using the same uri.
No errors are thrown. Just hangs.
const MongoClient = require('mongodb').MongoClient;
const uri = "mongodb+srv://user:password@main.9nwa88e.mongodb.net/?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();
});
Check my previous posts if you want an idea of what I've been dealing with.
I tried disabling my firewall and windows defender.
I made sure not to have < > around the username and password.
I also tried mongoose.
答案1
得分: 1
新的npm mongodb包不再使用提供的回调函数,而是MongoClient.connect(...)
返回一个Promise。这是一个重大变更。
英文:
The problem is, newer mongodb packages from npm do not exepect/call the provided callback anymore.
MongoClient.connect(...)
instead returns a promise.
This is a breaking change.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论