I have created a react app, and I have connected the mongoDB database, now the problem is whenever I start the node server the server gets started as well as the mongodb database, but most of the time the mongoDB database shows timeout error.
I am adding image, have a look through it for the error as well as I am showing my pacakage.json file.
{
"name": "ewk",
"version": "1.0.0",
"description": "abcd",
"main": "server.js",
"type": "module",
"scripts": {
"start": "node backend/server",
"server": "nodemon backend/server",
"client": "npm start --prefix frontend",
"dev": "concurrently "npm run server" "npm run client"",
"data:import": "node backend/seeder",
"data:destroy": "node backend/seeder -d"
},
"author": "xyz",
"license": "ISC",
"dependencies": {
"bcryptjs": "^2.4.3",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"express-async-handler": "^1.1.4",
"mongoose": "^5.11.13"
},
"devDependencies": {
"concurrently": "^5.3.0",
"nodemon": "^2.0.7"
}
}
This is my database configuration code:
import mongoose from 'mongoose';
const connectDB = async () => {
try{
const conn = await mongoose.connect(process.env.MONGO_URI, {
useUnifiedTopology: true,
useNewUrlParser: true,
useCreateIndex: true
})
console.log(`MongoDB Connected: ${conn.connection.host}`)
} catch (error){
console.error(`Error: ${error.message}`)
process.exit(1)
}
}
export default connectDB
I have added the uri which I added in env file:
mongodb+srv://abj:*******@ewfsa-cluster.hbwdr.mongodb.net/EWFSA?retryWrites=true&w=majority
question from:
https://stackoverflow.com/questions/66051271/cannot-connect-to-mongo-db-database-showing-timeout-error 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…