req.session.flash = { } 不工作 sails.js

req.session.flash = { } not working sails.js

我真的是 sails.js 的新手,目前我正在使用 sails 1.0。当我尝试验证我的注册页面时遇到问题,一旦表单无效,错误消息就会按预期显示在表单上方,但是当我进行刷新时它仍然存在。除此之外一切都还好。

路由是手动完成的,我认为问题出在 api/controllers/UsersController.js 中的 req.session.flash = {}; 这不是空对象!!

api/controllers/UsersControllers.js

module.exports = {

    signup:function(req,res,next){
        res.locals.flash = _.clone(req.session.flash);
        res.view('pages/signup');
        req.session.flash = {};
    },

    create:function(req,res,next){
        var values = req.allParams();
        Users.create(values).exec(function(err,user){
            // if there's an error
            if(err){
                console.log(err);
                req.session.flash = { 
                    err: err 
                }

            // if error redirect back to signup page
            return res.redirect('/users/signup');
            }

            //  After successfully creating the user
            // redirect to the show action

            res.view('pages/create');
            req.session.flash = {};
        });
    }
};

view/pages/signup.ejs

<div class="container">

    <% if(flash) { %>
      <ul class="alert alert-success">
        <% Object.keys(flash.err).forEach(function(error){ %>
          <li> <%- JSON.stringify(flash.err[error]) %> </li>
        <% }) %>
      </ul>
    <% } %>

  <div class="row login-pannel">
    <div class="col-md-4 col-md-offset-4">
      <div class="panel panel-default">
        <div class="panel-heading login-pannel-heading">
          <h3 class="panel-title"> User Signup </h3>
        </div>
        <div class="panel-body login-pannel-body">

          <form class="login-form" method="post" action="/users/create">

            <div class="form-group">
              <label for="loginUsername">Name</label>
              <input type="text" class="form-control" id="name" name="name" placeholder="Your Name" autofocus>
            </div>

            <div class="form-group">
              <label for="loginUsername">Email address</label>
              <input type="email" class="form-control" id="loginEmail" name="email" aria-describedby="emailHelp" placeholder="Your Email">
              <small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
            </div>

            <div class="form-group">
              <label for="loginPassword">Password</label>
              <input type="password" class="form-control" id="pass" name="password" placeholder="Password">
              <label class="text-danger" id="signup-pass"></label>
            </div>

            <div class="form-group">
              <label for="loginPassword">Confirm Password</label>
              <input type="password" class="form-control" id="con-pass" name="conPassword" placeholder="Confirm Password">
              <label class="text-danger" id="signup-con-pass"></label>
            </div>

            <button type="submit" class="btn btn-primary">Submit</button>
            <button type="reset" class="btn btn-danger reset-button">Reset</button>
            <input type='hidden' name='_csrf' value='<%= _csrf %>'>
          </form>

        </div>
      </div>
    </div>
  </div>
</div>

如果我的记忆没有出卖我,您必须在发送回复之前对您的 session object 进行任何更改。在你的 api/controllers/UsersControllers.js 中尝试交换这两行:

res.view('pages/create');
req.session.flash = {};

应该是:

req.session.flash = {};
res.view('pages/create');

阅读有关会议的更多信息here。最重要的是:

The property will not be stored in the session store nor available to other requests until the response is sent.