使用node.js 的express框架配置将session存储到mongodb时出错:
TypeError: Cannot read property 'Store' of undefined
express版本:3.3.4
connect-mongo:0.3.3
一开始按照网上的一些资料加载connect-mongo模块的写法为:
//加载模块
var MongoStore= require("connect-mongo");
//配置express session
app.use(express.session({
secret:setting.cookieSecret,
db: seeting.db
}));
启动程序报错:TypeError: Cannot read property 'Store' of undefined
经过排查和资料,查看connect-mongo的Readme.md,其中有段描述为:
With express:
var express = require('express');
var MongoStore = require('connect-mongo')(express);
app.use(express.session({
secret: settings.cookie_secret,
store: new MongoStore({
db: settings.db
})
}));
With connect:
var connect = require('connect');
var MongoStore = require('connect-mongo')(connect);
将var MongoStore= require("connect-mongo");修改为:
var MongoStore = require('connect-mongo')(express);
重启启动OK,问题解决!