Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: Connection strategy not found MongoDB

here is a simple connection to use express session store, it keeps banging out this error even though the text is right from the book. I am pretty sure is has something to do with 'new MongoStore' object initialization.

var express = require('express'),
    expressSession = require('express-session');

var MongoStore = require('connect-mongo/es5')(expressSession);
var sessionStore = new MongoStore({
  host: '127.0.0.1',
  port: '27017',
  db: 'session'
});

var app = express()
    .use(expressSession({
      secret: 'my secret sign key',
      store: sessionStore
     }))
    .use('/home', function (req, res) {
      if (req.session.views) {
        req.session.views++;
      }
      else {
        req.session.views = 1;
      }
      res.end('Total views for you:' + req.session.views);
    })
    .use('/reset', function(req, res) {
      delete req.session.views;
      res.end('Cleared all your views');
    })
    .listen(3000);
like image 313
Spencer Hire Avatar asked Jan 16 '16 01:01

Spencer Hire


4 Answers

Add URL to new MongoStore()

  var sessionStore = new MongoStore({
        host: '127.0.0.1',
        port: '27017',
        db: 'session',
        url: 'mongodb://localhost:27017/demo'
    });

The code in the question is from the book Beginning Node.js by Basarat Ali Syed.

like image 183
alturium Avatar answered Nov 04 '22 12:11

alturium


You need to define a database connection and then pass it to the new MongoStore. The 'db:' parameter you are using is expecting a MongoDB driver connect, not a url to a Mongo Database. For that, you should do something like this:

var sessionStore = new MongoStore({ url:'mongodb://localhost:27017/test');

Here's an example I know works, although it uses Mongoose instead of the MongoDB driver.

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/test');
var sessionStore = new MongoStore({mongooseConnection: mongoose.connection });
like image 1
Matthew Charles Avatar answered Nov 04 '22 11:11

Matthew Charles


Check the section Using Other Connect-Compatible Session Stores in http://sailsjs.com/documentation/reference/configuration/sails-config-session.

There are version restrictions regard the connect-mongo module and the example shows a different set of parameters than the ones in the documentation:

  // Note: user, pass and port are optional
  adapter: 'connect-mongo',
  url: 'mongodb://user:pass@host:port/database',
  collection: 'sessions',
  auto_reconnect: false,
  ssl: false,
  stringify: true

I actually only had to use:

  // Note: user, pass and port are optional
  adapter: 'connect-mongo',
  url: 'mongodb://localhost:27017/sails',
  collection: 'sessions'

Hope it helps!

like image 1
Davo Avatar answered Nov 04 '22 13:11

Davo


Since you're using a session collection so it should be like this

    app.use(expressSession({
      store: new mongoStore({
          mongooseConnection: mongoose.connection,
          collection: 'session',
      })
   }));
like image 8
Arman Ortega Avatar answered Nov 04 '22 12:11

Arman Ortega