这个错误通常是因为在使用MongoDB时,没有正确地初始化MongoStore。正确使用MongoStore的示例代码如下:
const session = require('express-session'); const MongoStore = require('connect-mongo')(session);
const store = new MongoStore({ url: 'mongodb://localhost:27017/mydb', // 连接mongodb的url collection: 'sessions', // 集合名 });
app.use(session({ secret: 'your secret string', // 用于session加密的字符串,可以随意填写 resave: true, saveUninitialized: true, store: store, }));
如果还是出现报错提示“Cannot read property 'create' of undefined”,可以检查一下MongoStore是否正确地引入并初始化。