我正在尝试将我的Express应用程序的会话存储在MongoStore中,但我在连接时收到错误。错误是一个我无法理解的巨大的Object / JSON,所以我尝试了我在网上找到的所有替代方案,但到目前为止还没有运气......
以下是应用配置:
var express = require('express'),
MongoStore = require('connect-mongo')(express),
passport = require('passport');
var app = express();
app.configure(function(){
app.use(express.compress());
app.use(express.static(path.join(__dirname, 'public')));
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.set('port', process.env.PORT || 8000);
app.use(express.favicon(__dirname + '/public/img/favicon.ico'));
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser('secret'));
app.use(express.session({
secret: 'secret',
store: new MongoStore({
db: dbOptions.db,
host: dbOptions.host,
port: dbOptions.port,
username: dbOptions.username,
password: dbOptions.password
},
function(err){
console.log(err || 'connect-mongodb setup ok');
})
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);
});
app.configure('development', function(){
app.use(express.errorHandler());
});
这是控制台中记录的错误:
{ db:
{ domain: null,
_events: {},
_maxListeners: 10,
databaseName: 'dbname',
serverConfig:
{ domain: null,
_events: {},
_maxListeners: 10,
_callBackStore: [Object],
_commandsStore: [Object],
auth: [Object],
_dbStore: [Object],
host: 'ec2-xx-xxx-xxx-xx.eu-west-1.compute.amazonaws.com',
port: 27017,
options: [Object],
internalMaster: true,
connected: true,
poolSize: 5,
disableDriverBSONSizeCheck: false,
_used: true,
replicasetInstance: null,
emitOpen: false,
ssl: false,
sslValidate: false,
sslCA: null,
sslCert: undefined,
sslKey: undefined,
sslPass: undefined,
serverCapabilities: [Object],
name: 'ec2-xx-xx-xxx-xx.eu-west-1.compute.amazonaws.com:27017',
_readPreference: null,
socketOptions: [Object],
logger: [Object],
eventHandlers: [Object],
_serverState: 'connected',
_state: [Object],
recordQueryStats: false,
socketTimeoutMS: [Getter/Setter],
db: [Circular],
dbInstances: [Object],
connectionPool: [Object],
isMasterDoc: [Object] },
options: { w: 1 },
_applicationClosed: false,
slaveOk: false,
bufferMaxEntries: -1,
native_parser: undefined,
bsonLib:
{ Code: [Function: Code],
Symbol: [Function: Symbol],
BSON: [Object],
DBRef: [Function: DBRef],
Binary: [Object],
ObjectID: [Object],
Long: [Object],
Timestamp: [Object],
Double: [Function: Double],
MinKey: [Function: MinKey],
MaxKey: [Function: MaxKey],
promoteLongs: true },
bson: { promoteLongs: true },
bson_deserializer:
{ Code: [Function: Code],
Symbol: [Function: Symbol],
BSON: [Object],
DBRef: [Function: DBRef],
Binary: [Object],
ObjectID: [Object],
Long: [Object],
Timestamp: [Object],
Double: [Function: Double],
MinKey: [Function: MinKey],
MaxKey: [Function: MaxKey],
promoteLongs: true },
bson_serializer:
{ Code: [Function: Code],
Symbol: [Function: Symbol],
BSON: [Object],
DBRef: [Function: DBRef],
Binary: [Object],
ObjectID: [Object],
Long: [Object],
Timestamp: [Object],
Double: [Function: Double],
MinKey: [Function: MinKey],
MaxKey: [Function: MaxKey],
promoteLongs: true },
_state: 'connected',
pkFactory:
{ [Function: ObjectID]
index: 16051204,
createPk: [Function: createPk],
createFromTime: [Function: createFromTime],
createFromHexString: [Function: createFromHexString] },
forceServerObjectId: false,
safe: false,
notReplied: {},
isInitializing: true,
openCalled: true,
commands: [],
logger: { error: [Function], log: [Function], debug: [Function] },
tag: 1387402758191,
eventHandlers:
{ error: [],
parseError: [],
poolReady: [],
message: [],
close: [] },
serializeFunctions: false,
raw: false,
recordQueryStats: false,
retryMiliSeconds: 1000,
numberOfRetries: 60,
readPreference: undefined },
collectionName: 'sessions',
internalHint: null,
opts: {},
slaveOk: false,
serializeFunctions: false,
raw: false,
readPreference: 'primary',
pkFactory:
{ [Function: ObjectID]
index: 16051204,
createPk: [Function: createPk],
createFromTime: [Function: createFromTime],
createFromHexString: [Function: createFromHexString] },
serverCapabilities: undefined }
这是非常模糊的,所以我不知道我应该在哪个方向搜索。
编辑1:
好的,谢谢,现在更有意义,但事情是我试图记录错误,因为会话没有存储在MongoDB中,我不明白为什么。我在MongoStore的设置周围添加了一个try catch块,看看是否有错误被触发而且没有,所以我不知道为什么它不起作用...
这就是passportjs存储和检索会话的方式:
passport.serializeUser(function(user, done) {
done(null, user._id);
});
passport.deserializeUser(function(_id, done) {
var userProvider = new UserProvider();
userProvider.findUser({ _id: _id }, function (err, users) {
if(users.length > 0) { done(null, users[0]); }
else { done(err, null); }
});
});
我错过了什么?
感谢您的帮助!
答案 0 :(得分:1)
您的问题在以下一行:
store: new MongoStore({
db: dbOptions.db,
host: dbOptions.host,
port: dbOptions.port,
username: dbOptions.username,
password: dbOptions.password
},
function(err){
console.log(err || 'connect-mongodb setup ok');
})
只需删除最后一个功能,我相信它应该按照您最初的预期工作
与Andrei在那里的答案相反,MongoStore构造函数确实接受了回调。但是它并不是出于错误处理的目的 - 它实际上传回了MongoStore的集合属性(这就是为什么console.log会说出它的作用)。如果出现错误,MongoStore只会抛出错误。您可以在源代码by clicking here中找到它。在第164行调用回调。