Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 739
Shaishab Roy Avatar asked May 13 '16 12:05

Shaishab Roy


People also ask

Why is my MongoDB not connecting?

These are some of the solutions: Ensure that your MongoDB instance is running: Compass must connect to a running MongoDB instance. Also check you have installed MongoDB and have a running mongod process. You should also check that the port where MongoDB is running matches the port you provide in the compass connect.

Why MongoDB Compass is not connecting?

This error often occurs when: You provide no hostname or an invalid hostname to the Compass connect dialog. The destination server rejects a connection on an incorrect port. Your MongoDB cluster or server has been shutdown or the server hostname has changed.


1 Answers

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 :)

like image 70
Shaishab Roy Avatar answered Nov 02 '22 18:11

Shaishab Roy