1 / 1
Apr 2024
DB_URI = mongodb+srv://user:xxx@cluster0.vrsuvuh.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0
const mongoose = require('mongoose'); const connectDatabase = () => { mongoose.connect(process.env.DB_URI, { useNewUrlParser: true, useUnifiedTopology: true }).then(con => { console.log(`MongoDB Database connected with HOST: ${con.connection.host}`); }).catch(err => { console.error('MongoDB connection error:', err); }); } module.exports = connectDatabase

Server started on PORT: 4000 in PRODUCTION mode.
MongoDB connection error: 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 (D:\Visual Studio\Visual Studio Code\Projects\Diplomna\shop\node_modules\mongoose\lib\connection.js:851:11)
at NativeConnection.openUri (D:\Visual Studio\Visual Studio Code\Projects\Diplomna\shop\node_modules\mongoose\lib\connection.js:826:11) {
reason: TopologyDescription {
type: ‘ReplicaSetNoPrimary’,
servers: Map(3) {
ac-wdjjxr2-shard-00-00.vrsuvuh.mongodb.net:27017’ => [ServerDescription],
ac-wdjjxr2-shard-00-01.vrsuvuh.mongodb.net:27017’ => [ServerDescription],
ac-wdjjxr2-shard-00-02.vrsuvuh.mongodb.net:27017’ => [ServerDescription]
},
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
setName: ‘atlas-10dpm3-shard-0’,
maxElectionId: null,
maxSetVersion: null,
commonWireVersion: 0,
logicalSessionTimeoutMinutes: null
},
code: undefined
}