我试图在用户注册失败后向其显示errorMessages。如果再次访问注册页面,则不会显示errorMessages数组。由于某种原因,在对注册页面的每个请求中都会显示req.session.errorMessages

module.exports.registration = function(req,res){

    if(Array.isArray(req.session.errorMessages) && req.session.errorMessages.length > 0)
        res.locals.errorMessages = req.session.errorMessages;
    else
        res.locals.errorMessages = [];

    res.render('registration',{

    });

    //This line does not seem to reset the array after the response has been rendered
    req.session.errorMessages = [];

};

最佳答案

将其移到render调用上方。将数据返回给用户后,您似乎无法再更改reqres

09-11 13:40