I am trying to connect to my database on mongodb and fetch some data from it. But right after establishing a connection the terminal stalls and I have to ctrl+C.
This is my code
const express = require('express');
const mongoose = require('mongoose');
const app = express();
const port = 3000;
const dbName = 'inventory';
const dbUrl = `mongodb://localhost:27017/${dbName}`;
// Define the schema and model for the "cars" collection
const carSchema = new mongoose.Schema({
make: String,
model: String,
year: Number
});
const Car = mongoose.model('Car', carSchema);
// Connect to the MongoDB server
mongoose.connect(dbUrl, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => {
console.log(`Connected to database '${dbName}'`);
// Define a GET route handler that fetches all cars from the database
app.get('/cars', async (req, res) => {
try {
const cars = await Car.find();
console.log(cars); // Log the fetched cars to the console
res.json(cars);
} catch (err) {
console.error(err);
res.status(500).send('Server error');
}
});
// Start the Express app
app.listen(port, () => {
console.log(`Server is listening on port ${port}`);
});
})
.catch((err) => {
console.error(err);
});
I know I have established a connection because I get this in my console
{"t":{"$date":"2023-05-13T21:12:56.152-06:00"},"s":"I", "c":"NETWORK", "id":51800, "ctx":"conn29","msg":"client metadata","attr":{"remote":"127.0.0.1:53075","client":"conn29","doc":{"driver":{"name":"nodejs|Mongoose","version":"5.3.0|7.1.1"},"platform":"Node.js v16.17.1, LE","os":{"name":"win32","architecture":"x64","version":"10.0.19045","type":"Windows_NT"}}}}
{"t":{"$date":"2023-05-13T21:13:06.654-06:00"},"s":"I", "c":"NETWORK", "id":22943, "ctx":"listener","msg":"Connection accepted","attr":{"remote":"127.0.0.1:53096","uuid":"9611eab4-7a75-49a0-9b44-d644599fe37c","connectionId":30,"connectionCount":18}}
{"t":{"$date":"2023-05-13T21:13:06.656-06:00"},"s":"I", "c":"NETWORK", "id":51800, "ctx":"conn30","msg":"client metadata","attr":{"remote":"127.0.0.1:53096","client":"conn30","doc":{"driver":{"name":"nodejs|Mongoose","version":"5.3.0|7.1.1"},"platform":"Node.js v16.17.1, LE","os":{"name":"win32","architecture":"x64","version":"10.0.19045","type":"Windows_NT"}}}}
I also get in my console log
Connected to database 'inventory'
Server is listening on port 3000
I feel like the problem is with my machine because I don’t know what else to try.
I was previously having an issue where my python app could connect to my mongodb but my node.js app couldn’t.
2
Answers
Your code seems correct to me, so I can only offer these advises:
I do some container stuff, and generally firewalls cause issues with database connection. Make sure those are configured on your machine and your db.
Also, check if you have permissions to read from the
inventory
database.Another troubleshooting step is to see if the
cars
collection is actually there.You can also try a debugger on your app to see where it exactly gets stuck at. Maybe the issue is in your code.
I would try to manually insert a document into the database, and then see if your setup can read it. Also, make sure the server is running correctly and that nothing else is running on port 3000, your firewall is configured correctly, etc.
Are you trying to use MongoDB cross-platform? For example, a linux terminal connecting to a windows hosted mongodb? If so, that will not work, at least not easily.
I had a situation where everything appeared to be working (windows mongodb compass and WSL terminal), but no documents were actually being inserted. The connection looked like it was there, but it was not. I fixed it by using windows powershell instead of WSL. When both the mongodb connection and the terminal were on Windows, it worked.