正如标题所示,尝试连接到mongodb地图集时出现错误。
错误:querySrv ENOTFOUND _mongodb._tcp.cluster0.yq9c1.mongodb.net
.env
ATLAS_URI=mongodb+srv://<username>:<pass>@cluster0.yq9c1.mongodb.net/<db>?retryWrites=true&w=majority
server.js
const express = require('express');
const cors = require('cors');
const mongoose = require('mongoose');
require('dotenv').config();
const server = express();
const port = 5000;
server.use(cors());
server.use(express.json());
const uri = process.env.ATLAS_URI;
mongoose.connect(uri, { useNewUrlParser: true, useCreateIndex: true })
.catch(err => console.log(`Error: ${err}`));
const connection = mongoose.connection;
connection.once('open', () => {
console.log('MongoDB connected');
})
.catch(err => console.log(`Error: ${err}`));
const userRouter = require('./routes/user');
server.use('/user', userRouter);
server.listen(port, () => {
console.log(`Server: ${port}`);
});
依赖项
"dependencies": {
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"mongoose": "^5.10.9",
"nodemon": "^2.0.6"
}
我过去以相同的方式连接了很多次,但是我现在(第一次)在Windows上进行开发,并且不确定是否忽略了一些简单的事情。再次检查密码。用户名数据库访问。网络访问。没了主意。