我在创建用户后尝试设置会话时收到错误:“无法设置未定义的属性'user'。”
exports.signUp=function(req,res){ // singup
new user({ //user model
username: req.body.username, // req body
}).save(function(error,user){ //After a successful save of user
**req.session.user=user.id;** // here is the error after a successful save
}
});
}
答案 0 :(得分:1)
您是否在节点应用中使用过类似的中间件?
app.use(session({
secret: 'botnyuserdetails', // session secret
resave: true,
saveUninitialized: true
}));
答案 1 :(得分:0)
exports.signUp=function(req,res){ // singup
new user({ //user model
username: req.body.username, // req body
}).save(function(error){
//**req.session.user=user.id;**
// save function doesn't return user
// you can use this approach
if(error){
res.json({error:"cannot save"})
}
else{
req.session.user = req.body.username;
}
}
});
}