gpt4 book ai didi

node.js - 将 MongoStore 与 Express 一起使用时出错

转载 作者:太空宇宙 更新时间:2023-11-04 01:08:19 26 4
gpt4 key购买 nike

我尝试将 Express 应用程序的 session 存储在 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:

好的,谢谢,现在更有意义了,但问题是我试图记录一个错误,因为 session 没有存储在 MongoDB 中,我不明白为什么。我在 MongoStore 的设置周围添加了一个 try catch block ,以查看是否触发了错误,但没有错误,所以我不知道为什么它不起作用......

这是 Passportjs 存储和检索 session 的方式:

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 的集合属性(这就是为什么 console.log 会输出它所做的事情)。如果出现错误,MongoStore 将简单地抛出错误。可以在源码中找到by clicking here 。回调在第 164 行调用。

关于node.js - 将 MongoStore 与 Express 一起使用时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20669180/

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