按照githhub上使用koa-session-mongoose,将session存入数据库,却发现无法给session写入值。
koa-session-mongoose的使用示例:
async function init (uri) => {
const connection = await mongoose.connect(uri, { useMongoClient: true });
app.use(session({
store: new MongooseStore({
collection: 'appSessions',
connection: connection,
expires: 86400, // 1 day is the default
name: 'AppSession'
})
}, app));
}
按照配置却一直设置不了session,最后查看了源码,发现这个配置示例应该是有问题的,修改好代码如下:
mongoose.connect("mongodb://some_host/some_db", { useNewUrlParser: true });
app.use(session({
store: new MongooseStore({
collection: 'appSessions',
// 这个是关键,connection的值是mongoose
connection: mongoose,
expires: 86400, // 1 day is the default
name: 'AppSession'
})
}, app));
各插件的版本如下:
"koa": "^2.5.2",
"koa-session": "^5.8.2",
"koa-session-mongoose": "^2.0.5",