Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

throw new Error('Required MongoStore option `db` missing');

I get the following when I start up my local node server.

Call to DB

mongoose.connect('mongodb://localhost/mays');
app.locals.db = mongoose.connection;

Error Message

C:\var\www\stage.mayfieldafc.com\node_modules\connect-mongo\lib\connect-mongo.js:157
    throw new Error('Required MongoStore option `db` missing');
          ^
Error: Required MongoStore option `db` missing

Output from Mongoos.connection

{ base:
   { connections: [ [Circular] ],
     plugins: [],
     models: { User: [Object] },
     modelSchemas: { User: [Object] },
     options: { pluralization: true } },
  collections:
   { users:
      { collection: null,
        opts: [Object],
        name: 'users',
        conn: [Circular],
        queue: [],
        buffer: true } },
  models:
   { User:
      { [Function: model]
        base: [Object],
        modelName: 'User',
        model: [Function: model],
        db: [Circular],
        discriminators: undefined,
        schema: [Object],
        options: undefined,
        collection: [Object] } },
  replica: false,
  hosts: null,
  host: 'localhost',
  port: 27017,
  user: undefined,
  pass: undefined,
  name: 'mays',
  options:
   { db: { read_preference: 'primary', forceServerObjectId: false, w: 1 },
     auth: {},
     server: { socketOptions: [Object], auto_reconnect: true },
     replset: { socketOptions: {} } },
  otherDbs: [],
  _readyState: 2,
  _closeCalled: false,
  _hasOpened: false,
  _listening: false,
  _events: {},
  db:
   { domain: null,
     _events: {},
     _maxListeners: 10,
     databaseName: 'mays',
     serverConfig:
      { domain: null,
        _events: {},
        _maxListeners: 10,
        _callBackStore: [Object],
        _commandsStore: [Object],
        auth: [Object],
        _dbStore: [Object],
        host: 'localhost',
        port: 27017,
        options: [Object],
        internalMaster: false,
        connected: false,
        poolSize: 5,
        disableDriverBSONSizeCheck: false,
        _used: true,
        replicasetInstance: null,
        emitOpen: true,
        ssl: false,
        sslValidate: false,
        sslCA: null,
        sslCert: undefined,
        sslKey: undefined,
        sslPass: undefined,
        serverCapabilities: null,
        name: 'localhost:27017',
        socketOptions: [Object],
        logger: [Object],
        eventHandlers: [Object],
        _serverState: 'connecting',
        _state: [Object],
        recordQueryStats: false,
        socketTimeoutMS: [Getter/Setter],
        db: [Circular],
        dbInstances: [Object],
        connectionPool: [Object] },
     options: { read_preference: 'primary', forceServerObjectId: false, w: 1 },
     _applicationClosed: false,
     slaveOk: false,
     bufferMaxEntries: -1,
     native_parser: undefined,
     bsonLib:
      { Code: [Object],
        Symbol: [Object],
        BSON: [Object],
        DBRef: [Object],
        Binary: [Object],
        ObjectID: [Object],
        Long: [Object],
        Timestamp: [Object],
        Double: [Object],
        MinKey: [Object],
        MaxKey: [Object],
        promoteLongs: true },
     bson: { promoteLongs: true },
     bson_deserializer:
      { Code: [Object],
        Symbol: [Object],
        BSON: [Object],
        DBRef: [Object],
        Binary: [Object],
        ObjectID: [Object],
        Long: [Object],
        Timestamp: [Object],
        Double: [Object],
        MinKey: [Object],
        MaxKey: [Object],
        promoteLongs: true },
     bson_serializer:
      { Code: [Object],
        Symbol: [Object],
        BSON: [Object],
        DBRef: [Object],
        Binary: [Object],
        ObjectID: [Object],
        Long: [Object],
        Timestamp: [Object],
        Double: [Object],
        MinKey: [Object],
        MaxKey: [Object],
        promoteLongs: true },
     _state: 'connecting',
     pkFactory:
      { [Function: ObjectID]
        index: 12355790,
        createPk: [Function: createPk],
        createFromTime: [Function: createFromTime],
        createFromHexString: [Function: createFromHexString],
        isValid: [Function: isValid],
        ObjectID: [Circular],
        ObjectId: [Circular] },
     forceServerObjectId: false,
     safe: false,
     notReplied: {},
     isInitializing: true,
     openCalled: true,
     commands: [],
     logger: { error: [Function], log: [Function], debug: [Function] },
     tag: 1422291172449,
     eventHandlers:
      { error: [],
        parseError: [],
        poolReady: [],
        message: [],
        close: [] },
     serializeFunctions: false,
     raw: false,
     recordQueryStats: false,
     retryMiliSeconds: 1000,
     numberOfRetries: 60,
     readPreference: undefined } }

This code is working on another machine. I am a little stumped to why I might get this error when I am supply a valid DB name that I can connect to via Robomongo

like image 342
Jamie Hutber Avatar asked Jan 26 '15 16:01

Jamie Hutber


Video Answer


2 Answers

Change the option you're passing to connect-mongo from

var session = require('express-session');
var MongoStore = require('connect-mongo')(session);
app.use(session({
    db: new MongoStore({ db: mongoose.connection.db })
}));

to

var session = require('express-session');
var MongoStore = require('connect-mongo')(session);
app.use(session({
   db: new MongoStore({ mongooseConnection: mongoose.connection })
}));

It's all documented in here: https://github.com/kcbanner/connect-mongo, really.

like image 194
FRD Avatar answered Sep 27 '22 21:09

FRD


Also, make sure you're up to date. In an older version the 'mongooseConnection' option was 'mongoose_connection'. Misnaming that triggers the db missing error.

like image 25
Jared Goldsmith Avatar answered Sep 27 '22 21:09

Jared Goldsmith