BUGS
Simple BUGS fix
▶️ Problem Statement
Sometimes you can get an error like this after running the command npm run dev
npm run dev
F:\mains\xamtrack>npm run dev
> xamtrack@1.0.0 dev
> nodemon app.js
[nodemon] 2.0.21
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node app.js`
MongooseServerSelectionError: Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you're trying to access the database from an IP that isn't whitelisted. Make sure your current IP address is on your Atlas cluster's IP whitelist: https://www.mongodb.com/docs/atlas/security-whitelist/
at _handleConnectionErrors (F:\mains\xamtrack\node_modules\mongoose\lib\connection.js:755:11)
at NativeConnection.openUri (F:\mains\xamtrack\node_modules\mongoose\lib\connection.js:730:11)
at async start (F:\mains\xamtrack\app.js:26:5) {
reason: TopologyDescription {
type: 'ReplicaSetNoPrimary',
servers: Map(3) {
'ac-8wysccb-shard-00-00.xknpt1w.mongodb.net:27017' => [ServerDescription],
'ac-8wysccb-shard-00-01.xknpt1w.mongodb.net:27017' => [ServerDescription],
'ac-8wysccb-shard-00-02.xknpt1w.mongodb.net:27017' => [ServerDescription]
},
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
setName: 'atlas-ebp80y-shard-0',
maxElectionId: null,
maxSetVersion: null,
commonWireVersion: 0,
logicalSessionTimeoutMinutes: null
},
code: undefined
}
[nodemon] clean exit - waiting for changes before restart
▶️ Solution
In this case, you need to whitelist your IP address in MongoDB Atlas. To do this, follow the steps below:
- Go to the MongoDB Atlas dashboard and click on the Network Access tab.
- Click on the Add IP Address button.
- Click on the Current IP Address button.
- Click on the Confirm button.
make sure you are logged in to your MongoDB Atlas account & delete the existing IP address from the list.
- Now go to Database tab and click on Connect button.
- Click on Connect your application button.
- Just copy from
@
to/
and replace it in your.env
file@cluster0.zdfdfdc.mongodb.net/
old
MONGODB_URL = mongodb+srv://xam:user789@xamtrack.xkgfgf1w.mongodb.net/xamtrack?retryWrites=true&w=majority
new
MONGODB_URL = mongodb+srv://xam:user789@cluster0.zdfdfdc.mongodb.net/?retryWrites=true&w=majority