hello dear community im wondering myself why i get this error when i try to use mongodb and nodejs.
const MongoClient = require('mongodb').MongoClient;
MongoClient.connect('mongodb://localhost:27017/TodoApp', (err, db) => {
if (err) {
console.log('Unable to connect to MongoDB server.')
}else{
console.log('Connected to MongoDB server.')
}
db.close();
});
Output in the console:
Unable to connect to MongoDB server. C:\Users\eljubec\Desktop\node-todo-api\node_modules\mongodb\lib\mongo_client.js:421 throw err ^
TypeError: Cannot read property 'close' of null at MongoClient.connect (C:\Users\eljubec\Desktop\node-todo-api\playground\mongodb-connect.js:9:8) at connectCallback (C:\Users\eljubec\Desktop\node-todo-api\node_modules\mongodb\lib\mongo_client.js:527:5) at C:\Users\eljubec\Desktop\node-todo-api\node_modules\mongodb\lib\mongo_client.js:418:11 at process._tickCallback (internal/process/next_tick.js:150:11)
dbis going to benull. Move the close command inside yourelseblock. - Chris Gerrto see what it is? - str