diff --git a/index.js b/index.js
index <HASH>..<HASH> 100644
--- a/index.js
+++ b/index.js
@@ -244,12 +244,12 @@ function runMongoMigrate(direction, migrationEnd) {
 	function performMigration(direction, migrateTo) {
 		var db = require('./lib/db');
 		db.getConnection(dbConfig || require(cwd + path.sep + configFileName)[dbProperty], function (err, db) {
-			var migrationCollection = db.migrationCollection,
-					dbConnection = db.connection;
 			if (err) {
 				console.error('Error connecting to database');
 				process.exit(1);
 			}
+			var migrationCollection = db.migrationCollection,
+					dbConnection = db.connection;
 
 			migrationCollection.find({}).sort({num: -1}).limit(1).toArray(function (err, migrationsRun) {
 				if (err) {
don't try to use db connection if error is thrown

Currently, if there are issues connecting to mongo
db.migrationCollection is still called. If db is undefined, this will
raise an exception. Moving error conditional up, so that an error is
logged instead.