Notes-Log about mongoDB access via mongoose errors after installation

Note: This post is a details-post for the post: Learning web app development through free online tutorials – Organized Notes and Log, https://raviswdev.blogspot.com/2024/03/learning-web-app-development-through.html .

Which version of mongoose got installed? 

Newer entry “npm view mongoose version” gives 8.0.3 (1st Jan 2024).

For this version, using the options specified in video for connect seems to trip up the program (lists out some errors). Instead, we can simply drop the options. The following code works (connects to MongoDB successfully).

mongoose
  .connect(connectionString)
  .then(() => console.log("Connected to DB ..."))
  .catch((err) => console.log(`Failed to connect to DB. ... ${err}`));

Older entry (see newer entry above): “npm view mongoose version” gave 8.0.1. So latest version is installed.

But I do get the following warnings on running Node.js projects video test program:
(node:4068) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
(node:4068) [MONGODB DRIVER] Warning: Top-level use of w, wtimeout, j, and fsync is deprecated. Use writeConcern instead.
(node:4068) [MONGODB DRIVER] Warning: Current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.

Following the video instructions fixes the issue. Resultant code:

mongoose
  .connect(connectionString, {
    useNewUrlParser: true,
    useCreateIndex: true,
    useFindAndModify: false,
    useUnifiedTopology: true,
  })
  .then(() => console.log("Connected to DB ..."))
  .catch((err) => console.log(err));

“npm install dotenv” if required. In my case it seems to have already been installed.

To check if a package is installed: npm list pkg-name

To view info. about a package: npm view pkg-name [I think this gives info. about the package irrespective of whether it is installed or not.]

For final version, the relevant instruction in README.MD is: “In order to run the project, setup .env and set MONGO_URI variable equal to DB connection string.”

Comments

Archive