From 357752da905559d6fb609b2eb91553f4c88977e9 Mon Sep 17 00:00:00 2001 From: Chris Brame Date: Mon, 1 Oct 2018 19:05:58 -0400 Subject: [PATCH] fix(database): removed incorrect events --- src/database/index.js | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) 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;