报错Cannot read property 'Store' of undefined
发布于 9 年前 作者 MartinHuangJing 4889 次浏览 最后一次编辑是 8 年前 来自 问答

报错卡了老半天了 求救 var app = express(); var session = require(‘express-session’); var connect = require(‘connect’); var SessionStore = require(‘session-mongoose’)(connect); var store = new SessionStore({ url:‘mongodb://localhost/session’, interval:120000 });

2 回复

上面这段代码本身没有问题。 问题可能和你其它代码有关。

@Changdao 但是我注掉var SessionStore = require(‘session-mongoose’)(connect);就好了; var express = require(‘express’); var path = require(‘path’); var favicon = require(‘serve-favicon’); var logger = require(‘morgan’); var cookieParser = require(‘cookie-parser’); var bodyParser = require(‘body-parser’); var routes = require(’./routes/index’); var users = require(’./routes/users’); var app = express();

//mongodb相关 var session = require(‘express-session’); var connect = require(‘connect’); var SessionStore = require(‘session-mongoose’)(connect); var store = new SessionStore({ url:‘mongodb://localhost/session’, interval:120000 });

// view engine setup app.set(‘views’, path.join(__dirname, ‘views’)); var ejs=require(‘ejs’);//新增
//添加以下支持解析HTML
app.engine(’.html’,ejs.__express);
app.set(‘view engine’, ‘html’);
//默认的ejs
//app.set(‘view engine’,‘ejs’);

// uncomment after placing your favicon in /public //app.use(favicon(__dirname + ‘/public/favicon.ico’)); app.use(logger(‘dev’)); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: false })); app.use(cookieParser()); app.use(express.static(path.join(__dirname, ‘public’)));

app.use(’/’, routes); app.use(’/users’, users);

// catch 404 and forward to error handler app.use(function(req, res, next) { var err = new Error(‘Not Found’); err.status = 404; next(err); }); // error handlers

app.use(session({ secret:‘test.com’, store:store, cookie:{maxAge:10000} }))

//用于把登录用户设置到res.locals里面,在home.html里显示//mongodb相关 app.use(function(req,res,next){ res.locals.user = req.session.user; console.log(‘Session is =’, req.session.user); next(); }) //mongodb相关 app.use(function(req,res,next){ res.locals.user = req.session.user; next(); }) //mongodb相关 app.use(function(req, res, next){ res.locals.user = req.session.user; var err = req.session.error; delete req.session.error; res.locals.message = ‘’; if (err) res.locals.message = ‘<div class=“alert alert-danger”>’ + err + ‘</div>’; next(); }); //mongodb相关

// development error handler // will print stacktrace if (app.get(‘env’) === ‘development’) { app.use(function(err, req, res, next) { res.status(err.status || 500); res.render(‘error’, { message: err.message, error: err }); }); }

// production error handler // no stacktraces leaked to user app.use(function(err, req, res, next) { res.status(err.status || 500); res.render(‘error’, { message: err.message, error: {} }); }); app.listen(3000); console.log(‘App Is Listening At Port 3000’); module.exports = app;

回到顶部