Sails.js flash message for user registration

后端 未结 3 975
不思量自难忘°
不思量自难忘° 2020-12-30 15:12

I am following Sail.js tutorial from http://irlnathan.github.io/sailscasts/blog/2013/08/27/building-a-sails-application-ep4-handling-validation-errors-with-a-flash-message/<

3条回答
  •  有刺的猬
    2020-12-30 15:37

    You can add this export:

    module.exports = function flash(req, res, next) {
      if (!req.session.flash) {
        req.session.flash = {};
        req.session.flash['success'] = [];
        req.session.flash['warning'] = [];
      }
      next();
    };
    

    and then in the view:

    <% if (req.session.flash['success'].length > 0) { %>
    
    <%= req.session.flash['success'].pop() %>
    <% } %> <% if (req.session.flash['warning'].length > 0) { %>
    <%= req.session.flash['warning'].pop() %>
    <% } %>

    You can create messages in the controller like this:

    req.session.flash['success'].push('Updated successfully');
    

提交回复
热议问题