I'm building an application that closes it's database connection after a certain time of inactivity. The problem is, I can't kill it's connection with MongoClient.close() because it's always returns that a error "TypeError: MongoClient.close is not a function". What am I doing wrong?
const MongoClient = require("mongodb").MongoClient;
let mongo = {};
let db;
let disconnectTimer;
let retryTimer;
let disconnect; //Used to prevent mongo from reconnect.
mongo.disconnect = () => {
disconnect = true;
try {
MongoClient.close();
} catch (error) {
console.warn("Error closing connection to Database =", error);
throw {
statusCode: 404,
error: error.toString(),
reason: "",
details: ""
};
}
}
mongo.getDB = () => {
if (typeof db !== "undefined" && !disconnect)
return db;
else
return connect().catch((error) => {
throw error
}).then((db) => {return db});
}
module.exports = mongo;
If db is used as the Mongo DB connection instance,then please try to use dB.close() instead of MongoClient.close().
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