Hello @53ac1da52812ebe8d38b9c0f38be5d9,
Thanks for sharing the detailed explanation.
The cause for this issue is that the repository uses an older version of the MongoDB node.js driver, specifically, version "mongodb": "^3.6.6"
. On the other hand, when you create your own project and install the MongoDB node.js driver package, it installs the latest version, which is "mongodb": "^5.3.0"
.
However, the latest version is incompatible with the code written in the tutorial because it uses the callback function which is deprecated in the new MongoDB node.js driver 5.0.0.
Reference: Changelog - node-mongodb-native.
If you want to follow the tutorial and build the project successfully, you have to use MongoDB version mongodb@4.16.0
or a lower version.
To resolve this problem and make your project functional, you must delete the node_module
directory and modify the MongoDB version in your package.json
file to "mongodb": "^4.16.0"
or a lower version.
Another workaround is to use the latest node.js driver version and modify the functions that use a callback
by switching to the Promise
and async/await
syntax instead.
As an example, you can update the content of the db/conn.js
file to incorporate async/await
. Sharing code snippet for your reference:
const { MongoClient } = require("mongodb");
const Db = process.env.ATLAS_URI;
const client = new MongoClient(Db, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
let _db;
module.exports = {
connectToServer: async function () {
try {
const db = await client.connect();
// Verify we got a good "db" object
if (db) {
_db = db.db("employees");
console.log("Successfully connected to MongoDB.");
}
return _db;
} catch (err) {
throw err;
}
},
getDb: function () {
return _db;
},
};
Similarly, you can modify the function in routes/record.js
.
I hope this helps. If you have any further questions, please let us know.
Best,
Kushagra