会话变量未加载到车把中

时间:2018-09-08 17:03:01

标签: javascript node.js express mongoose handlebars.js

app.js

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(session({
    secret: 'gh jewellery',
    resave: true,
    saveUninitialized: false,
    cookie: { secure: false }
}));
//app.use(function (req, res, next) {
//    res.locals.currentUser = req.session.user;
//    next();
//}); 

app.use('/', index);
app.use('/users', users);
app.use('/dashboard', dashboard);
app.use('/signup', signup);
app.use('/login', login);
app.use('/adreqform', adreqform);

在我的login.js中,这是我的路由文件,当命中端点/ login时,我设置了req.session.user

login.js

router.post('/', function (req, res, next) {
    User.findOne({ email: req.body.email }, function (err, user) {
        if (err) {
            console.log(err);
        } else if (!user) {
            res.render('login', { errnoacc: 'User does not exist! Please register.' })
        } else if (user && req.body.password == user.password) {
            req.session.user = user;
            req.session.save();
            //app.locals.currentUser = user;
            //console.log(req.app.locals);
            res.render('adreqform');
        } else {
            res.render('login', { errpass: 'Password does not match. Please try again!'});
        }

在我的布局文件中,它是车把。我想显示req.session.user,所以我{{user.username}}不起作用。

0 个答案:

没有答案