I’ve been working on a project intermittently for some time now. Recently, after transitioning to a new computer, and router none of my endpoints respond
I’ve taken several troubleshooting steps including:
- Deleting node_modules and reinstalling dependencies with npm.
- Adjusting inbound and outbound firewall rules for ports 27016.
- Attempting to app.listen on ports 27016 and 27017 in my application
setup. - Starting a new project from scratch.
- Adding extensive console logging for debugging purposes.
- Successfully connected to MongoDB database using the exact URI via MongoDB Shell and VS Code extensions;
- the initial connection in my app.js is established, and I can list databases
and collections without issue.
However, the mongoose connection to mongodb appears to “drop” unexpectedly when I make requests to my endpoints via postman.
//User.find and User.findOne timeout
router.get('/all', async (req, res) => {
try {
// Fetch all users
const users = await User.findOne({});
console.log(users);
// Respond with the list of users
res.send(users);
} catch (error) {
console.error('Error fetching users:', error);
res.status(500).json({ error: error.message });
}
});
“error”: “Operation users.findOne()
buffering timed out after 10000ms”
router.get('/check-connection', async (req, res) => {
try {
console.log('working')
console.log(mongoose.connection)
// Ensure the connection is established
if (mongoose.connection.readyState !== 1) {
throw new Error('MongoDB connection not established');
}
// Perform a basic query to the MongoDB server
const result = await mongoose.connection.useDb.admin().ping();
console.log('MongoDB Ping Result:', result);
res.status(200).send('Connection to MongoDB is successful');
} catch (error) {
console.error('Error pinging MongoDB:', error);
res.status(500).json({ error: 'Error connecting to MongoDB: ' + error.message });
}
});
“error”: “Error connecting to MongoDB: MongoDB connection not established”
Error pinging MongoDB: Error: MongoDB connection not established
Your insights on resolving this issue would be greatly appreciated.
more code below:
My app.js
https://playcode.io/1945377
my basic router – users.js
https://playcode.io/1945378