Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

531
Views
'MongoNotConnectedError' - I was trying to post data to mongodb and this error occured

I get the products when i console log the const products variable but i am having trouble to inserting data to mongodb database and its saying that mongoClient is not connected.

const express = require("express");
const cors = require("cors");
const { MongoClient } = require("mongodb");
require("dotenv").config();

const app = express();
app.use(cors());
app.use(express.urlencoded({ extended: true }));
app.use(express.json());

const uri = `mongodb+srv://${process.env.DB_USER}:${process.env.DB_PASSWORD}@cluster0.lgrhg.mongodb.net/${process.env.DB_NAME}?retryWrites=true&w=majority`;

const client = new MongoClient(uri, {
  useNewUrlParser: true,
  useUnifiedTopology: true,
});

client.connect((err) => {
  const productsCollection = client
    .db(`${process.env.DB_NAME}`)
    .collection(`${process.env.DB_COLLECTION}`);
  console.log("database connected")
  app.post("/addProduct", (req, res) => {
    const products = req.body;

    productsCollection.insertMany(products).then((result) => {
      console.log(result.insertedCount);
      res.send(result.insertedCount);
    });
  });
  client.close();
});

app.listen(4000);

After that i got this in my console:

database connected
MongoNotConnectedError: MongoClient must be connected to perform this operation
    at Object.getTopology (F:\Node-js\2-ema-john-server\node_modules\mongodb\lib\utils.js:389:11)
    at Collection.insertMany (F:\Node-js\2-ema-john-server\node_modules\mongodb\lib\collection.js:157:61)
    at F:\Node-js\2-ema-john-server\index.js:26:24
    at Layer.handle [as handle_request] (F:\Node-js\2-ema-john-server\node_modules\express\lib\router\layer.js:95:5)
    at next (F:\Node-js\2-ema-john-server\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (F:\Node-js\2-ema-john-server\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (F:\Node-js\2-ema-john-server\node_modules\express\lib\router\layer.js:95:5)
    at F:\Node-js\2-ema-john-server\node_modules\express\lib\router\index.js:281:22
    at Function.process_params (F:\Node-js\2-ema-john-server\node_modules\express\lib\router\index.js:335:12)
    at next (F:\Node-js\2-ema-john-server\node_modules\express\lib\router\index.js:275:10)
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

I got the answer :)

just remove client.close()

over 4 years ago · Santiago Trujillo Report

0

Can you try the below code snippet and let me know the output:

    client.connect((err) => {
  const productsCollection = client
    .db(`${process.env.DB_NAME}`)
    .collection(`${process.env.DB_COLLECTION}`);
  console.log("database connected")
  app.post("/addProduct", (req, res) => {
    const products = req.body;

    productsCollection.insertMany(products).then((result) => {
      console.log(result.insertedCount);
      client.close();
      res.send(result.insertedCount);
    });
  });
 
});
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!