I want to create multiple connections using mongoose. Therefore, I've chosen to use mongoose.createConnection()
instead of mongoose.connect()
.
When I connect on my localhost, I want to check if the database that I'm connecting to exists.
My connection to a non-existing database:
let db = mongoose.createConnection('mongodb://localhost/nonexistentdb');
Nevertheless, the events still give the the following results:
db.on('open', function() {
console.log("Opened"); // <= logged
});
db.on('connected', function () {
console.log('Connected'); // <= logged
});
db.on('error', (err) => {
console.log("Unable to connect to database"); // <= not logged
});
Note: It does log the error event on a non-existing database when using mongoose.connect()
.
How do I check for database existence using mongoose.createConnection()
?