Node.js连接到MongoDB Atlas时卡住。

huangapple go评论65阅读模式
英文:

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(&#39;mongodb&#39;).MongoClient;
const uri = &quot;mongodb+srv://user:password@main.9nwa88e.mongodb.net/?retryWrites=true&amp;w=majority&quot;;

MongoClient.connect(uri, function(err, db) {
  if (err) throw err;
  console.log(&#39;Connected to database!&#39;);

  // 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.

huangapple
  • 本文由 发表于 2023年5月15日 11:09:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76250656.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定