gpt4 book ai didi

javascript - 未处理的拒绝 MongoError : cannot connect to server in MongoDB

转载 作者:IT老高 更新时间:2023-10-28 13:31:28 26 4
gpt4 key购买 nike

运行我的 meanjs 项目时,我得到 Unhandled reject MongoError: cannot connect to server

以前运行良好,但更新时 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)

在配置文件代码中

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
})
}));

谁能帮帮我?

最佳答案

我终于知道发生错误的地方了!我发布这个答案是因为它可能会帮助其他人。

更新mongooseconnect-mongo版本时出现问题。

需要更新:

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

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

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

db: db.connection.db

现在使用

mongooseConnection: db.connection

这解决了我的问题:)

关于javascript - 未处理的拒绝 MongoError : cannot connect to server in MongoDB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37209915/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com