session设置不成功?
if(results[0].password === userObj.password){
results[0].teams.forEach(function(obj, i, arr){
if(obj.tag == true){
team.find({teamId: obj.teamId}, function (err, teams) {
console.log('hha',teams);
res.session.team = {
teamId: teams[0].name,
teamId: teams[0].teamId
}
})
}
});
req.session.user = {
userId: results[0].userId,
phone: results[0].phone
};
res.redirect('/liao');
}else{
req.flash('error','密码不正确');
res.redirect('/login');
return console.error('error password')
}
只有req.session.user可以设置成功。而req.session.team失败
6 回复
如果把req.session.team放到外面就可以设置,但是放外面我的值就取不到了啊😭
if(results[0].password === userObj.password){
results[0].teams.forEach(function(obj, i, arr){
if(obj.tag == true){
team.find({teamId: obj.teamId}, function (err, teams) {
console.log('hha',teams);
//req.session.team = {
// teamName: teams[0].name,
// teamId: teams[0].teamId
//}
})
}
});
req.session.user = {
userId: results[0].userId,
phone: results[0].phone
};
req.session.team = {
teamName: 'teams[0].name',
teamId: 'teams[0].teamId'
}
res.redirect('/liao');
}else{
req.flash('error','密码不正确');
res.redirect('/login');
return console.error('error password')
}
@liujavamail 循环里面不能做异步的查询吗?可是你写的这个不是也要放在循环里面吗?😭弄了一中午,还是感谢你指出我的问题,但是还没解决😭😭
求救😭
求助!!!为什么会存不成功呢?循环里面的req.session.team是在页面上存储成功了,但是数据库里只有req.session.user,求助求助😭
callback里面set team是异步的,不在当前的event loop中,res都结束了,再去set肯定没有吧, foreach多个要么放到一个batch operation中,要么promise.all(),然后在最后的callback里设置,res这在最后的callback里处理
@JasonBoy 多谢!