Reputation: 41
I am getting the below error in the terminal: Error: querySrv ECONNREFUSED _mongodb._tcp.cluster0.vxgqt.mongodb.net did not connect on running the index.js file It was working fine till yesterday and today on running it is giving this error. This is the code snippet:
import express from 'express';
import bodyParser from 'body-parser';
import mongoose from 'mongoose';
import cors from 'cors';
import postRoutes from './routes/posts.js';
const app = express();
app.use(bodyParser.json({ limit: '30mb', extended: true }))
app.use(bodyParser.urlencoded({ limit: '30mb', extended: true }))
app.use(cors());
app.use('/posts', postRoutes);
const CONNECTION_URL = 'mongodb+srv://<username>:<password>@cluster0.vxgqt.mongodb.net/myFirstDatabase?retryWrites=true&w=majority';
const PORT = process.env.PORT|| 5000;
mongoose.connect(CONNECTION_URL, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => app.listen(PORT, () => console.log(`Server Running on Port: http://localhost:${PORT}`)))
.catch((error) => console.log(`${error} did not connect`));
mongoose.set('useFindAndModify', false);
NOTE: I have provided the correct username and password in the connection url
Upvotes: 3
Views: 5879
Reputation: 11
Go to the network Access settings and click add ip address.This solved my problem
Upvotes: 1
Reputation: 41
I found a temporary fix and that is working: Under connect your application in MongoDB Atlas I selected node version 2.2.12 or later instead of 3.6 or later(which I used earlier). By selecting this, the application is now working fine as before
Upvotes: 1
Reputation: 1
you need to write your username and password inside URI of mongoDB, that mean u need to take value of CONNECTION_URL constant variable and change to current username of your account on mongoDB and change to current password of your account on mongoDB
Upvotes: 0
Reputation: 28366
querySrv ECONNREFUSED
means that the attempt to resolve the SRV record failed to connect to a name server.
This is a DNS problem. Your application will need to be able to resolve the SRV record for _mongodb._tcp.cluster0.vxgqt.mongodb.net
in order to use the mongodb+srv
connection string.
Upvotes: 1
Reputation: 1
First of all check your internet connection and then go to mongodb Atlas and check whether your ip address is available in whitelist or not. for testing purpose allow all ip's to access and then connect again.
Upvotes: 0