skip to Main Content

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

Answers


  1. Simply remove it. Your code should look like this

      const connectDB = async () => {
        try {
          const conn = await mongoose.connect(process.env.MONGO_URI);
        }
      }
    
    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search