question-mark
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://user:pass@cluster0-hcldh.mongodb.net/sample_airbnb';
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

7reactions
Yossitrxcommented, Feb 26, 2021

Getting the same thing when trying to preform:

app.post("/bitChange", async (req, res) => {
  const mongoClient = await client.connect();
  const dbo = mongoClient.db("leads_db");
  try {
    await dbo
      .collection("bit_rate")
      .updateOne(
        { name: "bit" },
        { $set: { change: req.body.change } },
        function (error, record) {
          if (error) throw error;
          res.json("done");
        }
      );
  } catch (e) {
    console.log("e");
    throw Error(e);
  } finally {
    client.close();
  }
1reaction
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 {
    if(client)
      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 Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found