gpt4 book ai didi

javascript - Socket.io sessionStore,store.get 方法不存在

转载 作者:可可西里 更新时间:2023-11-01 09:35:39 25 4
gpt4 key购买 nike

我正在尝试通过 Socket.io 和 passport.js 使用 session 存储

var session = require('express-session');
var cookieParser = require('cookie-parser');
var MongoStore = require('connect-mongo')(session);
var sessionStore = session({
store: new MongoStore({url: 'mongodb://localhost/RULE'}),
key: 'express.sid',
secret: 'Heisenberg'
});

// Socket.io auth
io.set('authorization', passportSocketIo.authorize({
cookieParser: cookieParser,
key: 'express.sid',
secret: 'Heisenberg',
store: sessionStore,
success: onAuthorizeSuccess,
fail: onAuthorizeFail,
}));

我得到这个错误:TypeError: auth.store.get 不是函数

所以我猜这是 connect-mongo 存储实例的问题,但我不知道如何调试它或可能是什么问题。

最佳答案

好的,所以问题是我提供的是 express session 而不是 MongoStore

var session = require('express-session');
var cookieParser = require('cookie-parser');
var MongoStore = require('connect-mongo')(session);
// sessionStore and expressSesion are now saved distinctly
var sessionStore = new MongoStore({url: 'mongodb://localhost/RULE'});
var expressSession = session({
store: sessionStore,
key: 'express.sid',
secret: 'ThisIsTheSecret'
});

var passportSocketIo = require('passport.socketio');

// Load middlewares
var morgan = require('morgan');

// Use middlewares
app.use(expressSession);

// Socket.io auth
io.set('authorization', passportSocketIo.authorize({
cookieParser: cookieParser,
key: 'express.sid',
secret: 'ThisIsTheSecret',
store: sessionStore, // here we pass the MongoStore, not the expressSession
success: onAuthorizeSuccess,
fail: onAuthorizeFail,
}));

关于javascript - Socket.io sessionStore,store.get 方法不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32999996/

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