I am not able to connect my database and getting this problem when i run any api call in postman ,I have tried so many things but not able to get it out .
CodePudding user response:
This is probably because
- The MongoDB service isn't started.
Start MongoDB:
sudo systemctl start mongodVerify that MongoDB has started successfully:
sudo systemctl status mongod
- The port is already serving some service.
Find pid:
lsof -i:27017Kill pid:
kill -9 pid
- Your connection string is incorrect
mongoose.connect('mongodb://localhost:27017/myapp');
- Your localhost is not configured to use mongoDB
CodePudding user response:
const app=require("./app");
const dotenv=require("dotenv");
const connectDatabase=require("./config/database");
PORT=4000;
//Handling uncaught Exception(YouTube Wala Error)
process.on("uncaughtException",(err)=>{
console.log(`Error:${err.message}`);
console.log(`Shuting Down the server due to uncaught Exception`);
process.exit(1);
})
// config
// dotenv.config({path:"backend/config/config.env"});
//Connecting to database
connectDatabase();
const server=app.listen(PORT,()=>{
console.log(`server is running on http://localhost:${PORT}`);
})
//Unhandled Promise Rejection-> in case when we use mongo(using)
process.on("unhandledRejection",err=>{
console.log(`Error: ${err.message}`);
console.log(`Shuting Down the server due to Unhandled Promise Rejection`);
server.close(() =>{
process.exit(1);
})
})
