En regardant la documentation de mongoose connect
Vous pouvez utiliser des Promesses.
var mongoose = require('mongoose');
var myModule = require('myModule');
var dbUrl = 'mongodb://localhost:27017/gfsTestDB';
mongoose.connect(dbUrl)
.then(
// The connection is ready to use!
() => {
var readyObj = new myModule(mongoose);
// ...
},
// Handle the connection error
(err) => {
// ...
},
);
Vous pouvez utiliser des rappels
var mongoose = require('mongoose');
var myModule = require('myModule');
var dbUrl = 'mongodb://localhost:27017/gfsTestDB';
mongoose.connect(dbUrl, (err) => {
if (err) {
// Handle the error
// ...
return;
}
// We get successfully connected to the database
var readyObj = new myModule(mongoose);
// ...
});