Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

don't call open again if it's already opened #94

Closed
wants to merge 2 commits into from

3 participants

@Sguo12

This gets rid of the exception when I pass in a mongoose db that's already opened.

@NicolasPelletier

Shouldn't this be

cb(null);
@NicolasPelletier

+1 on this.

I have an opened database already and I just want connect-mongo to use it as-is. My solution so far was not to pass any callback ( in which case _open_database() is not called ), but this is a trick I do because I saw in the code that it solved my problem. Using this patch is a much better approach.

@jdesboeufs jdesboeufs referenced this pull request from a commit
@jdesboeufs jdesboeufs Rewrite MongoStore initialization
* Re-use existing or upcoming mongoose connection
* Re-use existing or upcoming node-mongodb-native connection
* Accept full-featured MongoDB connection strings + advanced options
* Compatible with legacy config
* Replace callback by `connected` event
* Add debug

Fix #51, #58, #62, #66, #70, #85, #94, #96, #115, #117, #120
Fix #124, #128, #129, #130, #131, #133, #134
ca40b78
@jdesboeufs jdesboeufs closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 11, 2014
  1. don't call open again if it's already opened

    Steven Guo authored
Commits on Apr 24, 2014
  1. @Sguo12

    Update connect-mongo.js

    Sguo12 authored
This page is out of date. Refresh to see the latest.
Showing with 16 additions and 14 deletions.
  1. +16 −14 lib/connect-mongo.js
View
30 lib/connect-mongo.js
@@ -27,7 +27,7 @@ var defaultOptions = {host: '127.0.0.1',
};
module.exports = function(connect) {
- var Store = connect.session.Store;
+ var Store = connect.Store || connect.session.Store;
/**
* Initialize MongoStore with the given `options`.
@@ -171,21 +171,23 @@ module.exports = function(connect) {
};
this._open_database = function(cb){
- self.db.open(function(err, db) {
- if (err) {
- if (!(err instanceof Error)) { err = new Error(String(err)); }
- err.message = 'Error connecting to database: ' + err.message;
- throw err;
- }
+ if (!self.db.openCalled) {
+ self.db.open(function(err, db) {
+ if (err) {
+ throw new Error('Error connecting to database');
+ }
- if (options.username && options.password) {
- db.authenticate(options.username, options.password, function () {
+ if (options.username && options.password) {
+ db.authenticate(options.username, options.password, function () {
+ self._get_collection(cb);
+ });
+ } else {
self._get_collection(cb);
- });
- } else {
- self._get_collection(cb);
- }
- });
+ }
+ });
+ } else {
+ cb;
+ }
};
this.defaultExpirationTime = options.defaultExpirationTime || defaultOptions.defaultExpirationTime;
Something went wrong with that request. Please try again.