0

I was trying to learn MongoDB from a youtube tutorial.

https://youtu.be/fbYExfeFsI0

But when I was trying to insert data into my collection the same way as she did in the video, I get an output like this:

C:\Users\komal\Web Development\FruitsProject\node_modules\mongodb\lib\cmap\connection_pool.js:452 const error = this.closed ? new errors_1.PoolClosedError(this) : new errors_1.PoolClearedError(this); ^

PoolClosedError [MongoPoolClosedError]: Attempted to check out a connection from closed connection pool at ConnectionPool.processWaitQueue (C:\Users\komal\Web Development\FruitsProject\node_modules\mongodb\lib\cmap\connection_pool.js:452:45) at ConnectionPool.close (C:\Users\komal\Web Development\FruitsProject\node_modules\mongodb\lib\cmap\connection_pool.js:260:14) at Server.destroy (C:\Users\komal\Web Development\FruitsProject\node_modules\mongodb\lib\sdam\server.js:128:21) at destroyServer (C:\Users\komal\Web Development\FruitsProject\node_modules\mongodb\lib\sdam\topology.js:445:12) at node:internal/util:364:7 at new Promise (<anonymous>) at destroyServer (node:internal/util:350:12) at C:\Users\komal\Web Development\FruitsProject\node_modules\mongodb\lib\sdam\topology.js:226:56 at Function.from (<anonymous>) at Topology.close (C:\Users\komal\Web Development\FruitsProject\node_modules\mongodb\lib\sdam\topology.js:225:40) { address: 'ac-nw6d7vk-shard-00-01.ra2eh5p.mongodb.net:27017', [Symbol(errorLabels)]: Set(0) {} }

Node.js v18.12.0

My code in app.js file:


const { MongoClient } = require('mongodb');
async function main(){

const uri = "mongodb+srv://netTheCoder:[email protected]/?retryWrites=true&w=majority";
const client = new MongoClient(uri);


try{
    await client.connect();
    createListing(client,{
        name: "Hello Tower",
        summary: "A buitiful mesmarising building in Switzerland",
        bedroom: 30,
        bathrooms: 28
    })
   
} catch(e){
    console.log(e);
}finally{
    await client.close();
}
}

async function createListing(client,newListing){
   const result = await client.db("SampleDatabase").collection("Names").insertOne(newListing);
console.log(result);
}

main().catch(console.error);

I was trying to add data into a collection called Names in a database called SampleDatabase.

I tried changing databases and making new collections and also tried making new clusters. But none of it works. I have been struggling with this issue for a while now. Please help.

2 Answers 2

1

An async function implicitly returns a promise. I'm pretty sure createListing is actually a promise you forgot to await. Just add await in front of the createListing function.

const { MongoClient } = require('mongodb');
async function main(){

const uri = "mongodb+srv://netTheCoder:[email protected]/?retryWrites=true&w=majority";
const client = new MongoClient(uri);


try{
    await client.connect();
    await createListing(client,{  // <--- notice the await here
        name: "Hello Tower",
        summary: "A buitiful mesmarising building in Switzerland",
        bedroom: 30,
        bathrooms: 28
    })
   
} catch(e){
    console.log(e);
}finally{
    await client.close();
}
}

async function createListing(client,newListing){
   const result = await client.db("SampleDatabase").collection("Names").insertOne(newListing);
console.log(result); //<--- implicitly returns a promise
}

main().catch(console.error);

These are my understanding as far as I know. Any folks can let me know if I'm wrong.

More information on MDN async function

Sign up to request clarification or add additional context in comments.

Comments

0

I think the problem does not come from your database neither from your network … but from your client server.
Sometimes your client server gets CPU overloaded, all cores going to 100%. In those cases I guess it can’t handle in time its mongoDB requests which leads to this MongoPoolClearedError.

Anyone having this error should check on client side if the server is not overloaded.

1 Comment

In general, these observations are relevant in many cases. In this specific case, the example given misses an await for the createListing call. So the connection is closed before the query is executed. See the answer below.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.