My Error: useNewUrlParser has no effect since Node.js Driver version 4.0.0 and will be removed in the next major version

6.5k Views Asked by At

I am using mongoose to connect the MongoDB with my backend.

const connectDB = async () => { try { const conn = await mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true, });

This is the piece of code I wrote and it has 2 warning: "Warning: useNewUrlParser is a deprecated option" and "Warning: useUnifiedTopology is a deprecated option". Is there a way to fix them?

I tried installing version <4.0 but same error is picking up.

2

There are 2 best solutions below

0
On

The problem can be resolved by updating the MongoDB connection setup in Mongoose to use the latest default settings. Simply connect without specifying any additional options:

const mongoose = require('mongoose');

module.exports = async () => {
    try {
        await mongoose.connect(process.env.DB_URL, {});
        console.log("CONNECTED TO DATABASE SUCCESSFULLY");
    } catch (error) {
        console.error('COULD NOT CONNECT TO DATABASE:', error.message);
    }
};

#mdmahfuzrp #mahfuzrp #mrp

0
On

Simply remove it. Your code should look like this

  const connectDB = async () => {
    try {
      const conn = await mongoose.connect(process.env.MONGO_URI);
    }
  }