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.