Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

MongoError: Topology is closed, please connect

See original GitHub issue

Hi, I am receiving this error when inserting a documentMongoError: Topology is closed, please connect

This is my code

const monk = require('monk')
const url = 'mongodb+srv://';
const db = monk(url);
const lar = db.get('listingsAndReviews')

lar.insert({name: "Lovely Loft", summary: "A charming loft in Paris", bedrooms: 1, bathrooms: 1})

// db.then(() => {
//   console.log('Connected correctly to server') //this works
// }).catch((error) =>{
//   console.log(error);
// })

What have I done wrong?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:9
  • Comments:13

github_iconTop GitHub Comments

Yossitrxcommented, Feb 26, 2021

Getting the same thing when trying to preform:"/bitChange", async (req, res) => {
  const mongoClient = await client.connect();
  const dbo = mongoClient.db("leads_db");
  try {
    await dbo
        { name: "bit" },
        { $set: { change: req.body.change } },
        function (error, record) {
          if (error) throw error;
  } catch (e) {
    throw Error(e);
  } finally {
gh-andrecommented, Mar 25, 2021

MongoClient.close returns a promise and needs to be waited on to work without errors or warnings. A new instance of MongoClient needs to be allocated either via static MongoClient.connect or via instance connect, like this.

async function insertOne(doc: any) : Promise<void>
  let client: MongoClient | undefined;

  try {
    client = await MongoClient.connect(uri, {
                          useNewUrlParser: true,
                          useUnifiedTopology: true

    // OR
    client = new MongoClient(uri, {
                          useNewUrlParser: true,
                          useUnifiedTopology: true
    await client = client.connect();

    const col = client.db("mydb").collection("mycol");

    await col.insertOne(doc);
  finally {
      await client.close();
Read more comments on GitHub >

github_iconTop Results From Across the Web

MongoError: Topology is closed, please connect despite ...
I am writing a web application that uses asynchronous database requests as a part of the api. Currently, I have an async express...
Read more >
MongoTopologyClosedError: Topology is closed - MongoDB
Help When I am trying to connect to the MongoDB database it shows this error. Here I have submitted my index.js code and...
Read more >
MongoError: Topology is closed, please connect (client.close ...
Coding example for the question MongoError: Topology is closed, please connect (client.close and client.connect issue)-mongodb.
Read more >
[NODE-2544] Can't reuse mongo client after close
const client = new MongoClient(url, { useUnifiedTopology: true });. await client.connect ... MongoError: Topology is closed, please connect.
Read more >
MongoTopologyClosedError | mongodb
An error generated when an attempt is made to operate on a dropped, or otherwise unavailable, database. Hierarchy. MongoAPIError. MongoTopologyClosedError ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Post

No results found

github_iconTop Related Hashnode Post

No results found