Skip to content

Commit

Permalink
fix(mongo-client): move to inside of callback
Browse files Browse the repository at this point in the history
  • Loading branch information
Jessica Lord committed Nov 20, 2017
1 parent d44420b commit 68b0fca
Showing 1 changed file with 40 additions and 33 deletions.
73 changes: 40 additions & 33 deletions lib/mongo_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -848,31 +848,51 @@ var connect = function(self, url, options, callback) {
);
}

// Parse the string
var object;
parse(url, options, function(err, connectionStringObject) {
parse(url, options, function(err, object) {
if (err) return console.log(err);
object = connectionStringObject;
});
var _finalOptions = createUnifiedOptions({}, object);
_finalOptions = mergeOptions(_finalOptions, object, false);
_finalOptions = createUnifiedOptions(_finalOptions, options);

// Check if we have connection and socket timeout set
if (_finalOptions.socketTimeoutMS == null) _finalOptions.socketTimeoutMS = 360000;
if (_finalOptions.connectTimeoutMS == null) _finalOptions.connectTimeoutMS = 30000;
// Parse the string
var _finalOptions = createUnifiedOptions({}, object);
_finalOptions = mergeOptions(_finalOptions, object, false);
_finalOptions = createUnifiedOptions(_finalOptions, options);

if (_finalOptions.db_options && _finalOptions.db_options.auth) {
delete _finalOptions.db_options.auth;
}
// Check if we have connection and socket timeout set
if (_finalOptions.socketTimeoutMS == null) _finalOptions.socketTimeoutMS = 360000;
if (_finalOptions.connectTimeoutMS == null) _finalOptions.connectTimeoutMS = 30000;

// Store the merged options object
self.s.options = _finalOptions;
if (_finalOptions.db_options && _finalOptions.db_options.auth) {
delete _finalOptions.db_options.auth;
}

// Failure modes
if (object.servers.length === 0) {
throw new Error('connection string must contain at least one seed host');
}
// Store the merged options object
self.s.options = _finalOptions;

// Failure modes
if (object.servers.length === 0) {
throw new Error('connection string must contain at least one seed host');
}

// Do we have a replicaset then skip discovery and go straight to connectivity
if (_finalOptions.replicaSet || _finalOptions.rs_name) {
return createReplicaset(
self,
_finalOptions,
connectHandler(self, _finalOptions, connectCallback)
);
} else if (object.servers.length > 1) {
return createMongos(
self,
_finalOptions,
connectHandler(self, _finalOptions, connectCallback)
);
} else {
return createServer(
self,
_finalOptions,
connectHandler(self, _finalOptions, connectCallback)
);
}
});

function connectCallback(err, topology) {
if (err && err.message === 'no mongos proxies found in seed list') {
Expand All @@ -895,19 +915,6 @@ var connect = function(self, url, options, callback) {
// Return the error and db instance
callback(err, topology);
}

// Do we have a replicaset then skip discovery and go straight to connectivity
if (_finalOptions.replicaSet || _finalOptions.rs_name) {
return createReplicaset(
self,
_finalOptions,
connectHandler(self, _finalOptions, connectCallback)
);
} else if (object.servers.length > 1) {
return createMongos(self, _finalOptions, connectHandler(self, _finalOptions, connectCallback));
} else {
return createServer(self, _finalOptions, connectHandler(self, _finalOptions, connectCallback));
}
};

module.exports = MongoClient;

0 comments on commit 68b0fca

Please sign in to comment.