connect-mongodb icon indicating copy to clipboard operation
connect-mongodb copied to clipboard

TypeError: Cannot call method 'findOne' of null at MongoStore.get

Open erhangundogan opened this issue 14 years ago • 11 comments

Hi. I'm using [email protected] and getting "TypeError: Cannot call method 'findOne' of null at MongoStore.get" after updated to [email protected], [email protected]. I guess your mongodb version 0.9.7 is rather old one.

erhangundogan avatar Mar 17 '12 13:03 erhangundogan

Yup. Ditto here. It seemed completely broken. Turned out I was not calling the constructor properly. Old example code strikes again.

I thought I should use new mongoStore({ db: db }) where the 2nd db is the db global created when I fire up Mongoose. But that's not right. Instead, I tried new mongoStore({ url: myDbUrl }) and it works.

danmactough avatar Mar 21 '12 06:03 danmactough

Caught me as well. Old, confusing docs.

hunterloftis avatar Apr 12 '12 14:04 hunterloftis

same prob, any news here?

kof avatar May 31 '12 10:05 kof

@kof the solution by danmactough with db url as parameter works for me, too.

andreek avatar Jun 03 '12 10:06 andreek

my issue was, that connection was used before it is ready, after I started to use a callback it stoped throw errors. I suppose queries will not be queued until connection is established like in mongoose.

kof avatar Jun 03 '12 10:06 kof

Will this method work when using MongoDB in a replicaset and the URL specifies multiple hosts? I did not see any indication in the code that it does.

JerryLuke avatar Jun 13 '12 22:06 JerryLuke

Stumbled across this obviously using Mongoose and unsure of where the expected connect-mongodb db object is. The proposed solution to use the db url, would open 2 connections as far as I can tell. Ideally there'd be a way to get the expected db object out of mongoose and reuse it rather than setting up a separate connection.

Will follow-up if I find it.

CrabBot avatar Aug 29 '12 23:08 CrabBot

@CrabBot I also wanted to use an existing connection rather than creating a separate one and the solution that worked for me was to use db: mongoose.connection.db in mongoStore. I also had to ensure that the database connection was established first before setting up the session store. Here's what I did:

var mongoose = require('mongoose');
var mongoStore = require('connect-mongodb');
db = mongoose.connect(DB_URI);

mongoose.connection.on('open', function() {
  app.use(express.session({
    store: new mongoStore({
      db: mongoose.connection.db,
      collection : 'sessions'
    })
  }));
});

lern avatar Oct 01 '12 20:10 lern

@lern Thanks. I'll check that out.

CrabBot avatar Oct 01 '12 21:10 CrabBot

@lern Thank you so much! After hours of headache it finally works :)

stefanszymanski avatar Oct 30 '12 12:10 stefanszymanski

Just a follow-up, @lern's solution works.

CrabBot avatar Dec 20 '12 19:12 CrabBot