新手求解 Cant set headers after they are sent
同一个用户登录后执行后面两个电影列表页和用户列表页都没有问题,就是在执行后台录入页出现Cant set headers after they are sent 用户权限统一为5.
//相关代码如下
//后台录入页
app.get('/admin/movie/add', User.signinRequired, User.adminRequired, Movie.add);
//电影列表页
app.get('/admin/movie/list', User.signinRequired,User.adminRequired, Movie.list);
//用户列表页
app.get('/admin/user/list',User.signinRequired,User.adminRequired, User.list);
//shouwSignin
app.get('/signin', User.showSignin);
//showSignup
app.get('/signup', User.showSignup);
====================================================
//登录用户权限
exports.signinRequired = function(req, res, next) {
var user = req.session.user;
console.log(' signin user in session:' + user);
if (!user) {
return res.redirect('/signin');
}
next();
}
//管理员权限
exports.adminRequired = function(req, res, next) {
var user = req.session.user;
User.findOne({name:user.name},function(err,result){
if(err){
console.log(err);
}
if(result.role <= 10){
return res.redirect('/signin');
}
});
next();
}
//后台录入页面
exports.add = function (req, res) {
res.render('admin', {
title: 'imooc 后台录入页面',
movie: {
doctor: '',
country: '',
title: '',
year: '',
poster: '',
language: '',
flash: '',
summary: ''
}
});
}
//电影列表页
exports.list = function (req, res) {
Movie.fetch(function (err, movies) {
if (err) {
console.log(err)
}
res.render('list', {
title: 'imooc 列表页',
movies: movies
})
})
}
//用户列表页
exports.list = function(req,res){
User.fetch(function (err, users) {
if (err) {
console.log(err)
}
res.render('userlist', {
title: 'imooc 用户列表页',
users:users
})
})
}
// showSignup
exports.showSignup = function(req, res) {
res.render('signup', {
title: '注册'
})
}
//showSignin
exports.showSignin = function(req, res) {
res.render('signin', {
title: '登录'
})
}
=================================================================== 还有个现象就是在访问后台登入页面时,先是控制台报Cant set headers after they are sent错误,但是浏览器还是能到这种页面,再次刷新浏览器就无法加载了,当程序跑到adminRequired不是已经不满足管理员权限而应该返回登录页面,不应该继续再访问movie.add方法了吗。求解,想了很久没想通