无法将 nodejs 连接到 mongoDB Atlas

Having trouble connecting nodejs to mongoDB Atlas

我在连接 nodejs 和 atlas 时遇到问题。

这是我的代码

var mongoose = require('mongoose')
var mongoDB =
    'mongodb+srv://Yeon:yeoyeon1@cluster0.nnzsw.mongodb.net/local_library?retryWrites=true&w=majority'
mongoose.connect(mongoDB, { useNewUrlParser: true, useUnifiedTopology: true })
var db = mongoose.connection
db.on('error', console.error.bind(console, 'MongoDB connection error: '))

这是错误代码

MongoDB connection error:  Error: querySrv ETIMEOUT _mongodb._tcp.cluster0.nnzsw.mongodb.net
    at QueryReqWrap.onresolve [as oncomplete] (dns.js:203:19) {
  errno: undefined,
  code: 'ETIMEOUT',
  syscall: 'querySrv',
  hostname: '_mongodb._tcp.cluster0.nnzsw.mongodb.net'
}

我像这样从 mongodb+src 切换到 mongodb 错误码只是变长了,但没有解决

var mongoDB =
    'mongodb://Yeon:dusrjf4346@cluster0.nnzsw.mongodb.net/local_library?retryWrites=true&w=majority'

我已经完成了所有这三个。

First Make sure your IP is whitelisted like the above suggestions. The easiest solution is the "All IPs" whitelist of 0.0.0.0/0

Second Check your VPN if you are using one. I was trying to connect to the cluster while connected to a VPN. Once I turned off my VPN, I was able to connect.

Third Make sure your internet settings are not preventing you from connecting to the cluster. My home Xfinity wifi security settings were set too high and this was preventing the connection. I think the router was not allowing a connection to port 27017. One way to test if this issue is occurring is to tether your computer to your phone for internet instead of your wifi and try connecting. I was able to connect using my iPhone as a hotspot for internet. I reset my router to factory settings which fixed the issue.

但是,没有解决我的问题。
我认为我的密码和数据库名是正确的

使用这个

const mongoose = require("mongoose");

mongoose.connect("MONGO_URI", {
  useNewUrlParser: true,
  useCreateIndex: true,
  useUnifiedTopology: true,
});

const connection = mongoose.connection;
connection.once('open', () => {
  console.log('MongoDB database connection established successfully');
});