I am seeing my mongoose pool seemingly close before it inserts data as I am getting this error when making a call to my mongoose db in my cloud cluster
MongoRuntimeError: Connection pool closed
but I am awaiting all of the calls? so I'm unsure why I'm seeing this issue, maybe it has something to do with the way I am defining my client? hopefully someone has some tips or ideas on this
export const storeData = async (data) =>{
const uri = `mongodb+srv://plantmaster:${password}@cluster0.yey8l.mongodb.net/plantstore?retryWrites=true&w=majority`;
const client = await MongoClient.connect(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
serverApi: ServerApiVersion.v1
});
const newPLantData = { name: "Company Inc", address: "Highway 37" };
await client.db("plantstore").collection("plantdata").insertOne(newPLantData, (err, res) =>{
if(err) throw err;
console.log(result)
})
await client.close();
};
I am calling this function on an express post route like so
// store data
app.post('/store', async function (req, res) {
await storeData(req.body);
res.send('data was stored')
})
i was facing the same error, but i fixed it by waiting 1500ms before closing the connection.
setTimeout(() => {client.close()}, 1500)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With