我正在尝试将我的 Express 应用程序的会话存储在 MongoStore 中,但连接时出现错误。错误是一个巨大的对象/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); }
});
});
我错过了什么?
谢谢你的帮助!