I am new in MongoDB. When I run the code below, I get an errror: Topology is closed. I want to create a document in collection. Why does this happen and how can I fix it? I would appretiate any help.
const { MongoClient } = require('mongodb');
const uri = "mongodb+srv://(login info)@cluster0.56skb.mongodb.net/myFirstDatabase?retryWrites=true&w=majority";
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });
client.connect(err => {
const db = client.db("test");
async function mongo () {
await db.collection('a').insertMany([
{ item: "journal",
qty: 25,
size: { h: 14, w: 21, uom: "cm" },
status: "A"},
{ item: "notebook",
qty: 50,
size: { h: 8.5, w: 11, uom: "in" },
status: "A"},
{ item: "paper",
qty: 100,
size: { h: 8.5, w: 11, uom: "in" },
status: "D"},
{ item: "planner",
qty: 75, size: { h: 22.85, w: 30, uom: "cm" },
status: "D"},
{ item: "postcard",
qty: 45,
size: { h: 10, w: 15.25, uom: "cm" },
status: "A"}
])
.then(function(result) {
// process result
})
}
mongo()
const collection = client.db("test").collection("devices");
// perform actions on the collection object
client.close();
});
1- Check your database name and password.
2- Open Mongodb atlas and in "Network Access", change the IP address as "0.0.0.0/0 (includes your current IP address) ".
3- Maybe you can try adding "await" before client.close().