diff --git a/src/database/index.js b/src/database/index.js index de902e7e4..48df95b67 100644 --- a/src/database/index.js +++ b/src/database/index.js @@ -30,25 +30,23 @@ module.exports.init = function(callback, connectionString, opts) { if (opts) options = opts; if (!_.isUndefined(process.env.MONGOHQ_URL)) CONNECTION_URI = process.env.MONGOHQ_URL.trim(); - mongoose.connection.once('error', function(e) { - winston.error('Oh no, something went wrong with DB! - ' + e.message); - db.connection = null; - return callback(e, null); - }); + if (db.connection) + return callback(null, db); - mongoose.connection.once('connected', function() { + mongoose.Promise = global.Promise; + mongoose.connect(CONNECTION_URI, options).then(function() { if (!process.env.FORK) winston.info('Connected to MongoDB'); db.connection = mongoose.connection; - return callback(null, db); - }); - if (db.connection) return callback(null, db); + }).catch(function(e) { + winston.error('Oh no, something went wrong with DB! - ' + e.message); + db.connection = null; - mongoose.Promise = global.Promise; - mongoose.connect(CONNECTION_URI, options).catch(function(e) { return callback(e, null); }); + return callback(e, null); + }); }; module.exports.db = db;