I am not being able to connect to MongoDB.
The error i am getting is
| options.includeResultMetadata ??= false;
20:46:01 0|app | ^^^
20:46:01 0|app | SyntaxError: Unexpected token '??='
Node version when i am running the project is
node -v => v20.7.0
MongoDB version
mongod –version => db version v6.0.8
package.json
"dependencies": {
"compression": "^1.7.4",
"cors": "^2.8.5",
"express": "^4.18.2",
"helmet": "^7.0.0",
"http-status": "^1.7.0",
"mongodb": "^6.1.0",
"xss-clean": "^0.1.4"
}
}
What am i missing
The code
const { MongoClient } = require('mongodb');
const uri = 'mongodb://localhost/testDatabase';
const client = new MongoClient(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
async function connectToDatabase() {
try {
await client.connect();
console.log('Connected to the MongoDB database');
} catch (err) {
console.error('Error connecting to the MongoDB database:', err);
}
}
module.exports = {
connectToDatabase,
getDatabase: () => client.db(),
};
2
Answers
same problem, until mongo release a patch for version 6.1.0
this worked for me… (downgrading the mongodb javascript module) as @selfagency suggested
npm uninstall mongodb
then
npm i [email protected]
other earlier versions are available
https://www.npmjs.com/package/mongodb/v/5.9.0?activeTab=versions
you might also edit your package.json dependencies
"mongodb": "^6.1.0",
to say
"mongodb": "^5.9.0",
and reinstall you applications dependencies. depends if you are deploying or developing locally