shaishab roy shaishab roy - 24 days ago 9
Javascript Question

Unhandled rejection MongoError: cannot connect to server in MongoDB

I got

Unhandled rejection MongoError: cannot connect to server
when run my meanjs project.

Previously was running fine but when update
nodejs 5.11.1
,
mongodb 3.2.6
,
"mongoose": "^4.4.16"
and
"connect-mongo": "^1.1.0",
then this error getting.


Unhandled rejection MongoError: cannot connect to server
at Collection.listIndexes (/home/shaishab/Project/Open-Source/BS-Commerce/node_modules/mongodb/lib/collection.js:1750:11)
at indexInformation (/home/shaishab/Project/Open-Source/BS-Commerce/node_modules/mongodb/lib/db.js:1625:25)
at Db.indexInformation (/home/shaishab/Project/Open-Source/BS-Commerce/node_modules/mongodb/lib/db.js:1589:44)
at ensureIndex (/home/shaishab/Project/Open-Source/BS-Commerce/node_modules/mongodb/lib/db.js:1082:8)
at Db.ensureIndex (/home/shaishab/Project/Open-Source/BS-Commerce/node_modules/mongodb/lib/db.js:1058:44)
at ensureIndex (/home/shaishab/Project/Open-Source/BS-Commerce/node_modules/mongodb/lib/collection.js:1820:13)
at Collection.ensureIndex (/home/shaishab/Project/Open-Source/BS-Commerce/node_modules/mongodb/lib/collection.js:1808:44)
at MongoStore.setAutoRemoveAsync (/home/shaishab/Project/Open-Source/BS-Commerce/node_modules/connect-mongo/src/index.js:123:40)
at MongoStore.handleNewConnectionAsync (/home/shaishab/Project/Open-Source/BS-Commerce/node_modules/connect-mongo/src/index.js:116:18)
at MongoStore (/home/shaishab/Project/Open-Source/BS-Commerce/node_modules/connect-mongo/src/index.js:95:26)
at module.exports (/home/shaishab/Project/Open-Source/BS-Commerce/config/express.js:101:10)
at Object. (/home/shaishab/Project/Open-Source/BS-Commerce/server.js:31:38)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)


in config file code

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

var db = mongoose.connect('dbUrl');
app.use(session({
saveUninitialized: true,
resave: true,
secret: config.sessionSecret,
store: new MongoStore({
db: db.connection.db,
collection: config.sessionCollection
})
}));


Can any one help me ?

Answer

Finally I got where error occurred!! I post this answer because of it may will help others.

Problem occurred when update mongoose and connect-mongo version.

need to update:

MongoStore = require('connect-mongo')(session)

and

app.use(session({
        saveUninitialized: true,
        resave: true,
        secret: config.sessionSecret,
        store: new MongoStore({
            mongooseConnection: db.connection,
            collection: config.sessionCollection
        })
    }));

main problem was in MongoStore({db: db.connection.db instead of use

db: db.connection.db

now using

mongooseConnection: db.connection

that solved my problem :)

Comments