Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with connecting mongodb driver 2.0.x #161

Closed
barisusakli opened this issue Mar 3, 2015 · 14 comments
Closed

Problem with connecting mongodb driver 2.0.x #161

barisusakli opened this issue Mar 3, 2015 · 14 comments
Milestone

Comments

@barisusakli
Copy link

Mongodb 3.0
node-mongodb-native 2.0.19
connect-mongo 0.7.0

When I run a query on the database it never calls the callback. If I remove the connect mongo init code it works fine. Here is the relevant code.

var session = require('express-session'),
var sessionStore = require('connect-mongo')(session);

mongoClient.connect(connString, connOptions, function(err, _db) {

    // if the below 3 lines are commented out the findOne query works properly
    module.sessionStore = new sessionStore({
       db: _db
    });

   _db.collection.findOne({_key:"someKey"}, {}, function(err, data) {
         console.log('never reaches here');
    });
});
@barisusakli
Copy link
Author

Seems like options.db.openCalled is undefined no matter what. If I set it to true before I create the session store the queries work.

@jdesboeufs
Copy link
Owner

Most of the connection strategies are not tested yet :(
I think the problem is related to node-mongodb-native 2.0.

@franckl
Copy link

franckl commented Mar 13, 2015

I have a similar issue, I upgraded to mongodb native 2.0.21 from 1.4.32 and it stopped working. My code is the same as @barisusakli

Edit: I am using mongo 2.6.8

@barisusakli
Copy link
Author

A temp work around that works for me

mongoClient.connect(connString, connOptions, function(err, db) {
    db.openCalled = true
    module.sessionStore = new sessionStore({
       db: db
    });
    //rest of code

@jdesboeufs
Copy link
Owner

I'm on it.

@jdesboeufs jdesboeufs changed the title mongodb 3.0 query problem Problem with connecting mongodb driver 2.0.x Mar 21, 2015
@jdesboeufs jdesboeufs modified the milestone: 1.0.0 Mar 21, 2015
@1999
Copy link
Contributor

1999 commented Mar 22, 2015

@barisusakli oh does it? I have this king of log:

  connect-mongo switched to state: init +0ms
  connect-mongo use strategy: `native_db` +1ms
  connect-mongo switched to state: connected +0ms
  connect-mongo switched to state: connecting +0ms

initWithNativeDb(); -> process.nextTick(initWithNativeDb); seems to fix the issue. Also it seems like db.open doesn't fire callback.

@1999
Copy link
Contributor

1999 commented Mar 22, 2015

Made a little fix in #165

@mjmasn
Copy link

mjmasn commented Mar 31, 2015

I'm passing an existing Db instance to connect-mongo. The fix in #165 doesn't work for me, I still have to set db.openCalled = true (but only for external database hosted on compose, a database running on localhost works fine)

@jdesboeufs
Copy link
Owner

How can we detect the state of this existing Db instance in mongodb driver 2.0?

@1999
Copy link
Contributor

1999 commented Mar 31, 2015

I still have to set db.openCalled = true (but only for external database hosted on compose, a database running on localhost works fine)

yep, same thing.

How can we detect the state of this existing Db instance in mongodb driver 2.0?

It seems like "fullsetup" event can be used for that, I'll try to have a look

@1999
Copy link
Contributor

1999 commented Mar 31, 2015

As far as I see, db.open() shouldn't be run directly now:
https://github.com/mongodb/node-mongodb-native/blob/2771d2a34527a0783fbfc7bda0b7a5604f10c30a/lib/collection.js#L29-L44

The proof of this is that connect-mongo works nice when db.openCalled is set to true and db.collection() runs inside initWithNativeDb.

And here are some links:

1.4 has openCalled property set to true when open() has been called:
https://github.com/mongodb/node-mongodb-native/blob/02bf8ece1227b0c35448fe16f5d0073de26106a7/lib/mongodb/db.js#L266

2.0 doesn't have this property at all:
https://github.com/mongodb/node-mongodb-native/blob/2771d2a34527a0783fbfc7bda0b7a5604f10c30a/lib/db.js#L188-L202

@chapati
Copy link

chapati commented Apr 22, 2015

Huh, I killed the whole day for this issue. Why not to mention in docs? Your 5 minutes will save time for people.

@1999
Copy link
Contributor

1999 commented Apr 23, 2015

@chapati did v0.8.1 help you or not?

@chapati
Copy link

chapati commented Apr 23, 2015

@1999 Haven't checked yet. Figured out the same workaround with openCalled = true as suggested above. Will try v0.8.1 soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants