[nodemon] restarting due to changes...
[nodemon] starting `ts-node src/index.ts`
Server running on port undefined
Servidor Funcionando en el puerto: 4000
Failed to connect to MongoDB: 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/
[nodemon] app crashed - waiting for file changes before starting...
Julio Acuña is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
This error usually indicates one of two issues:
Your MongoDB Atlas IP whitelist is not set up correctly
Your environment variables (your port or MongoDB URI) are misconfigured
Here’s a checklist of common steps to resolve the problem:
-
Whitelist your IP Address in MongoDB Atlas
Log in to MongoDB Atlas and open your Cluster.
Go to the Network Access tab.
Under the IP Access List, make sure to add your current IP address (or 0.0.0.0/0 if you are just testing and want to allow access from anywhere, though that’s less secure).
Once added, wait a moment for Atlas to apply the new IP access settings.
Re-run your application.
Note: Ensure that the connection string in your .env or configuration file matches the connection string given in the “Connect” dialog in Atlas, including the username and password. -
Check Your Environment Variables
Check your PORT: It looks like your console is showing:Server running on port undefined
This suggests the environment variable for your port might not be set. Make sure you have something like:
typescript
const PORT = process.env.PORT || 4000;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
Check your MongoDB URI: Often stored in an .env file as something like:
MONGODB_URI=mongodb+srv://<username>:<password>@yourcluster.mongodb.net/<database>?retryWrites=true&w=majority
Then in your code:
typescript
import mongoose from 'mongoose';
mongoose
.connect(process.env.MONGODB_URI as string, { /* options */ })
.then(() => {
console.log('Connected to MongoDB');
})
.catch((error) => {
console.error('Failed to connect to MongoDB:', error);
});
Yash Tiwari is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.