'MongoNotConnectedError: MongoClient must be connected to perform this operation. How to handle this error?

cmd How can i solve this. Just showing MongoClient must be connected to perform this operation at getTopology ..... at collection.insertOne .... at maybePromise..

const express = require('express');
const {MongoClient} = require('mongodb');
const password = 'NLJXkuGFhUd68@9';



const uri = "mongodb+srv://organicUser:NLJXkuGFhUd68@[email protected]/organicdb? 

retryWrites=true&w=majority"; const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });

const app = express();

app.get('/', (req, res) => {
    res.send("Hello, I am Working");
})



client.connect(err => {
  const collection = client.db("organicdb").collection("products");

  // perform actions on the collection object
  const product = {name: "Modhu", price: 120, quantity: 30};
  collection.insertOne(product)
  .then(result => {
      console.log("One product added");
  })
  console.log("database connected");

});


app.listen(8000)


Solution 1:[1]

Just comment the code or remove client.close()

 finally {
           // await client.close();
        }

Solution 2:[2]

Solved. Just replace @ with %40 of the password

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Hayeul Tanjib
Solution 2 Mehedi Hasan