Home > Blockchain >  How can I get rid of this MongoDB error? mongoose MongoNotConnectedError: MongoClient must be connec
How can I get rid of this MongoDB error? mongoose MongoNotConnectedError: MongoClient must be connec

Time:12-04

Please I am using the MongoDB database for my next application that is my final project for me Bootcamp and there is this error that has prevented me from making queries to my database as I always get the mongoclient not connected error. I am using mongoose and this error started after I upgraded to the latest mui(material UI) because that is what I am using for this application. I have been trying since yesterday to fix this error as I thought it is something I could handle but till this moment it persist. It has been going from this mongoose MongoNotConnectedError: MongoClient must be connected to perform this operation and this one MongoExpiredSessionError: Cannot use a session that has ended` and it happens on every button that clicks that makes a request to the database. Below is the code I am using to connect to MongoDB with mongoose:

    import mongoose from 'mongoose';



const connection = {};


async function connect() {

  if (connection.isConnected) {
    console.log('already connected');
    return;
  }

  if (mongoose.connections.length > 0) {
    connection.isConnected = mongoose.connections[0].readyState;



    if (connection.isConnected === 1) {
      console.log('use previous connection');
      return;
    }

    await mongoose.disconnect();
  }

  const db = await mongoose.connect(process.env.MONGODB_URI, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
  });

  console.log('new connection');

  connection.isConnected = db.connections[0].readyState;
}


async function disconnect() {
  if (connection.isConnected) {
    if (process.env.NODE_ENV === 'production') {
      await mongoose.disconnect();
      connection.isConnected = false;
    } else {
      console.log('not disconnect');
    }
  }
}


function convertDocToObj(doc) {
  doc._id = doc._id.toString();
  doc.createdAt = doc.createdAt.toString();
  doc.updatedAt = doc.updatedAt.toString();

  return doc;
}

const db = { connect, disconnect, convertDocToObj };

export default db;


I will really appreciate it so much if anybody can help me out with this problem, please. I don't know what is causing it or where it is coming from as I have tried to as much as i can to all to no0 avail Thanks

CodePudding user response:

I see that you are using the mongo URL as an environment variable with MONGODB_URI.

Do you have the dotenv module installed? Did you require('dotenv').config() in your applications? is the `.env file in the root directory of your project?

If so, if you are using MongoDB Atlas, make sure the URL in your .env file is the correct one. When you generate the connection string from MongoDB Atlas it gives a default DB in the URL string named myFirstDatabase or something like that. Change it to the DB you want to connect to.

  • Related