未处理的拒绝MongoError:无法连接到MongoDB中的服务器

时间:2021-03-23 07:01:12

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

我得到了Unhandled rejection MongoError:运行my meanjs项目时无法连接到服务器。

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.

以前运行正常,但更新nodejs 5.11.1,mongodb 3.2.6,“mongoose”:“^ 4.4.16”和“connect-mongo”:“^ 1.1.0”,然后这个错误得到。

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)

未处理的拒绝MongoError:无法在indexInformation(/ home / shaishab /)上的Collection.listIndexes(/home/shaishab/Project/Open-Source/BS-Commerce/node_modules/mongodb/lib/collection.js:1750:11)连接到服务器项目/开源/ BS-Commerce / node_modules / mongodb / lib / db.js:1625:25)在Db.indexInformation(/ home / shaishab / Project / Open-Source / BS-Commerce / node_modules / mongodb / lib / db.js:1589:44)在Db.ensureIndex(/ home / shaishab /)的ensureIndex(/home/shaishab/Project/Open-Source/BS-Commerce/node_modules/mongodb/lib/db.js:1082:8)项目/开源/ BS-Commerce / node_modules / mongodb / lib / db.js:1058:44)在ensureIndex(/ home / shaishab / Project / Open-Source / BS-Commerce / node_modules / mongodb / lib / collection。 js:1820:13)在MongoStore.setAutoRemoveAsync(/ home / shaishab /)的Collection.ensureIndex(/home/shaishab/Project/Open-Source/BS-Commerce/node_modules/mongodb/lib/collection.js:1808:44)项目/开源/ BS-Commerce / node_modules / connect-mongo / src / index.js:123:40)在MongoStore.handleNewConnectio MongoStore的nAsync(/home/shaishab/Project/Open-Source/BS-Commerce/node_modules/connect-mongo/src/index.js:116:18)(/ home / shaishab / Project / Open-Source / BS-Commerce /node_modules/connect-mongo/src/index.js:95:26)在Object的module.exports(/home/shaishab/Project/Open-Source/BS-Commerce/config/express.js:101:10)。 (/home/shaishab/Project/Open-Source/BS-Commerce/server.js:31:38)在Module._compile(module.js:413:34)的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 ?

谁能帮我 ?

2 个解决方案

#1


16  

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.

更新mongoose和connect-mongo版本时出现问题。

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

主要问题是MongoStore({db:db.connection.db而不是使用

db: db.connection.db

now using

mongooseConnection: db.connection

that solved my problem :)

那解决了我的问题:)

#2


0  

As I did not have a db variable, my solution was:

由于我没有db变量,我的解决方案是:

var mongoStore = require('connect-mongo')(session);
var mongoose = require('mongoose');

const connection = mongoose.createConnection('mongodb://localhost/chester');

app.use(session({
    secret: config.secrets.session,
    resave: true,
    saveUninitialized: true,
    store: new mongoStore({mongooseConnection: connection})
  }));

hope it helps

希望能帮助到你

#1


16  

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.

更新mongoose和connect-mongo版本时出现问题。

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

主要问题是MongoStore({db:db.connection.db而不是使用

db: db.connection.db

now using

mongooseConnection: db.connection

that solved my problem :)

那解决了我的问题:)

#2


0  

As I did not have a db variable, my solution was:

由于我没有db变量,我的解决方案是:

var mongoStore = require('connect-mongo')(session);
var mongoose = require('mongoose');

const connection = mongoose.createConnection('mongodb://localhost/chester');

app.use(session({
    secret: config.secrets.session,
    resave: true,
    saveUninitialized: true,
    store: new mongoStore({mongooseConnection: connection})
  }));

hope it helps

希望能帮助到你