Stack Exchange
error occuring while using mongoose.disconnect
I am newbie in mongodb/mongoose and javascript. On using mongoose.disconnect it throws an error

return new error_1.MongoNetworkError('connection establishment was cancelled');
^
MongoNetworkError: connection establishment was cancelled "

here is my code -
const mongoose = require("mongoose");

const mongoDB = "mongodb://127.0.0.1:27017/fruitsDB";

mongoose
    .connect(mongoDB,{
        useNewUrlParser:true,useUnifiedTopology:true
    })
    .then(() => console.log("MongoDB connected"))
    .catch((err) => console.error("error!!! - ",err));

const fruitSchema = new mongoose.Schema ({
    name:String,
    rating:Number,
    review:String
});

const Fruit = mongoose.model("Fruit",fruitSchema);

const fruit = new Fruit ({
    name:"Apple",
    rating:7,
    review:"pretty solid as a fruit"
});

// fruit.save();

const personSchema = new mongoose.Schema ({
    name:String,
    age:Number
});

const Person = mongoose.model("Person",personSchema);

const person = new Person ({
    name:"xyz",
    age:37
});

person.save();

const kiwi = new Fruit({
    name:"Kiwi",
    score:10,
    review:"The best fruit"
});

const orange = new Fruit({
    name:"Orange",
    score:4,
    review:"Too sour for me"
});

const banana = new Fruit({
    name:"Banana",
    score:10,
    review:"Weird texture"
});

// Fruit
//     .insertMany([kiwi,orange,banana])
//     .then( ()=> console.log("Succesfully saved all the fruits to fruitsDB"))
//     .catch( (err) => console.log(err));

async function getFruits() {
    const fruit = await Fruit.find({});
    mongoose.disconnect();
    return fruit;
}

const printFruitsName = async () => {
    const res = await getFruits();
    res.forEach((fruit) => console.log(fruit.name));
}

printFruitsName();

please help !
tldr: tried to exit mongodb using mongoose.disconnect() but it throws error that connection wasnt established
Is their anyway I can close the mongoose database connection

we're you able to connect to your MongoDB server via another client to validate it works? you you would get such error when there is a network / auth problem

@Deano yes I was able to connect to MongoDB from the terminal . It all works great until I add mongoose.disconnect( )

mongoose.connect is asynchronous, so your application can continue on with the following line as soon as the call begins.
Since the connection will likely take a second or two to be established, the application can progress through and likely reach the .disconnect long before the connection is completed.
To fix this example, move all of the code that comes after to inside the .then callback so that it doesn't begin to execute until the connection has been established.

thanks sir , can you give an example . Do I have to move all the code that comes after .connect part to .then ?

For a quick adjustment, move  console.log("MongoDB connected"))     .catch((err) => console.error("error!!! - ",err)); to the bottom of the file.
