MongoError:无法建立拓扑功能,因为驱动程序仍在连接Server.capabilities [英] MongoError: cannot establish topology capabilities as driver is still in process of connecting at Server.capabilities
问题描述
我正在尝试通过mongoose.connect连接到mongoDB,并且继续出现错误:
I'm trying to connect to mongoDB via mongoose.connect and I continue to get the error:
/Users/Documents/Business/01000100/node_modules/connect-mongo/lib/connect-mongo.js:133
throw err;
^
MongoError: cannot establish topology capabilities as driver is still in process of connecting
at Server.capabilities
auth_server.js:
auth_server.js:
var express = require('express')
var body_parser = require('body-parser')
var cookie_parser = require('cookie-parser')
var express_session = require('express-session')
var mongo_store = require('connect-mongo')({session:express_session})
var mongoose = require('mongoose')
var morgan = require('morgan')
var port = 8080
//MODELS
require('./models/user_model.js')
//CONFIG
mongoose.connect('mongodb://localhost/db')
var app = express()
//VIEW ENGINE
app.engine('.html', require('ejs').__express)
app.set('views', __dirname + '/views')
app.set('view engine', 'html')
//MIDDLEWARE
app.use(morgan('dev'))//logging requests
app.use(body_parser.urlencoded({extended: true}));
app.use(cookie_parser())
app.use(express_session({
secret: '2!H$,Br2&1XW74zpd897ytf lbph=-0987654edfvbn5Q4AQ0]k7XX2Plh915ZV2)0)2DvHK}4KA"^6J!TY;x4z04',
cookie: {maxAge: 60 * 60 * 1000},
store: new mongo_store({
db: mongoose.connection.db,
collection: 'sessions'
}),
resave: false,
saveUninitialized: false
}))
//ROUTES
require('./routes')(app)
//START
app.listen(port)
console.log('Santa is listening on ' + port)
有人知道导致此问题的原因或解决方法吗?我花了很多时间进行研究,但我似乎仍然无法找到解决方案.提前非常感谢您.
Does anyone know what is causing this problem or how to fix it? I've spent a bunch of hours on it researching and I still cannot seem to pinpoint a solution. Thank you very much in advance.
推荐答案
某些事情以及不直接使用mongoose方法的会话存储信息有可能在建立连接之前尝试访问数据库.猫鼬方法本身会将其隐藏起来,并排队"操作,直到实际建立连接为止.
Something, and what is likely the session store information, which does not use the mongoose methods directly is trying to access the database before the connection has been established. The mongoose methods themselves hide this away and "queue" the operations until after the connection is actually made.
在"connection"事件中包装所有应用程序启动,以确保已建立连接:
Wrap all your application startup in the "connection" event to make sure an connection has been established:
//CONFIG
mongoose.connect('mongodb://localhost/db'); // Does not wait for connection here
var app = express();
mongoose.connection.on("connect",function(err) { // But this waits for connection
// All Setup here - But especially this
app.use(express_session({
secret: '2!H$,Br2&1XW74zpd897ytf lbph=-0987654edfvbn5Q4AQ0]k7XX2Plh915ZV2)0)2DvHK}4KA"^6J!TY;x4z04',
cookie: {maxAge: 60 * 60 * 1000},
store: new mongo_store({
db: mongoose.connection.db,
collection: 'sessions'
}),
resave: false,
saveUninitialized: false
}))
//START
app.listen(port)
console.log('Santa is listening on ' + port)
})
这篇关于MongoError:无法建立拓扑功能,因为驱动程序仍在连接Server.capabilities的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!