TypeError: Cannot call method 'findOne' of null at MongoStore.get
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.
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.
Caught me as well. Old, confusing docs.
same prob, any news here?
@kof the solution by danmactough with db url as parameter works for me, too.
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.
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.
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 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 Thanks. I'll check that out.
@lern Thank you so much! After hours of headache it finally works :)
Just a follow-up, @lern's solution works.