title: How can I get rid of this MongoDB error? mongoose MongoNotConnectedError: MongoClient must be connected to perform this operation
body: <p>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 <code>mongoose MongoNotConnectedError: MongoClient must be connected to perform this operation and this one </code>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:</p>
  <pre><code>    import mongoose from 'mongoose';
  
  
  
  const connection = {};
  
  
  async function connect() {
  
    if (connection.isConnected) {
      console.log('already connected');
      return;
    }
  
    if (mongoose.connections.length &gt; 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;
  
  
  </code></pre>
  <p>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</p>