我试图将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); }
});
});
我想念什么?
谢谢你的帮助!
最佳答案
您的问题在以下行中:
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的collection属性(这就是console.log吐出它所做的事情的原因)。如果有错误,MongoStore只会抛出一个错误。您可以在源代码by clicking here中找到它。该回调在第164行被调用。