Sails.js 0.10.0-RC4无法将消息闪烁到EJS视图



在服务器JS

 if (!user) {
      if (isEmail) {
        req.flash('error', 'Error.Passport.Email.NotFound');
        sails.log.warn('User Email not fond.');
      } else {
        req.flash('error', 'Error.Passport.Username.NotFound');
        sails.log.warn('User name not found.');
      }

ejs view

<form role="form" action="/auth/local" method="post">
    <input type="text" name="identifier" placeholder="Username or Email">
    <input type="password" name="password" placeholder="Password">
    <button type="submit">Sign in</button>
</form>
<% if (typeof message!== 'undefined') { %>
<%= message %>
<% } else { %>
You E-mail and passport is correct!
<% } %>

如果我输入了一个错误的电子邮件或护照,则EJS视图Donot显示任何错误消息,为什么?如何将错误消息刷到EJS视图?我做错了什么?对不起,我的英语不好。thx。

实际上, req会自动传递到您的视图,因此在您的视图中,您可以做:

<%- req.flash('message') %>

您无需手动将消息传递到视图。

Flash Middleware将Flash消息存储在会话中。您仍然必须将其传递给您的视图并独自渲染:

app.get('/flash', function(req, res){
  // Set a flash message by passing the key, followed by the value, to req.flash().
  req.flash('info', 'Flash is back!')
  res.redirect('/');
});
app.get('/', function(req, res){
  // Get an array of flash messages by passing the key to req.flash()
  res.render('index', { messages: req.flash('info') });
});

使用 req.session.flash检查存在消息的情况正在给出 undefined sailsjs v0.10.0.0-rc8。

这是我最终使用的解决方案:

  <% var errors = req.flash('error'); %>
    <% if ( Object.keys(errors).length > 0 ) { %>
    <div class="alert alert-danger">
      <button type="button" class="close" aria-hidden="true" data-dismiss="alert">&times;</button>
      <ul>
        <% Object.keys(errors).forEach(function(error) { %>
          <li><%- JSON.stringify(errors[error]) %></li>
        <% }) %>
      </ul>
    </div>
   <% } %>

update

我在阅读消息之前想出了如何检查闪存消息的存在:

   <% if (req.session.flash && req.session.flash.error) { %>
      <% var errors = req.flash('error') %>
      <div class="alert alert-danger">
        <button type="button" class="close" aria-hidden="true" data-dismiss="alert">&times;</button>
        <ul>
          <% Object.keys(errors).forEach(function(error) { %>
            <li><%- JSON.stringify(errors[error]) %></li>
          <% }) %>
        </ul>
      </div>
   <% } %>

我发现以下自定义验证的GIST方法更有帮助。我想您最终无论如何都需要自定义验证消息,对吗?

https://gist.github.com/basco-johnkevin/8436644

注意:我创建了一个名为api/services/vilenationservice.js的文件,并在此处放置了要点代码。这样,我不必在控制器中要求它。

在控制器中:

create: function(req, res, next) {
    //Create a User with the params sent from the signup form
    //
    User.create( req.params.all(), function userCreated( err, user ) {
        // If there's an error
        // 
        if(err) {
            if(err.ValidationError) {
                errors = ValidationService.transformValidation(User, err.ValidationError);
                sails.log.warn(errors);
                req.flash('error', errors);
            }
            // If error redirect back to the sign-up page
            return res.redirect('/user/new');
        }
        // After successfully creating the user 
        // redirect the to the show action
        res.json(user);
    });
}

在我的用户模型中:

module.exports = {
  schema: true,
  attributes: {
    name: {
        type: 'string',
        required: true
    },
    title: {
        type: 'string'
    },
    email: {
        type: 'string',
        email: true,
        required: true,
        unique: true
    },
    encryptedPassword: {
        type: 'string',
        minLength: 6,
        required: true
    }
  },
  validation_messages: {
    name: {
        required: 'You must supply a valid name.'
    },
    email: {
        email: 'You must supply a valid email address.',
        required: 'You must supply a valid email address.',
        unique: 'An account with this email address already exists.'
    },
    encryptedPassword: {
        minLength: 'Your password must be atleast 6 characters long.',
        required: 'You must supply a password that is atleast 6 characters long.'
    }
  }
};

这是我在用户注册表单视图上最终所了解的。嗯...一定有更好的方法。

<% if (req.session.flash && req.session.flash.error) { %>
  <% var errors = req.flash('error') %>
  <div class="alert alert-danger">
    <button type="button" class="close" aria-hidden="true" data-dismiss="alert">&times;</button>
    <ul>
      <% Object.keys(errors).forEach(function(error) { %>
        <% Object.keys(errors[error]).forEach(function(error_message) { %>
        <% Object.keys(errors[error][error_message][0]).forEach(function(error_message_res) { %>
        <li>
            <%- errors[error][error_message][0][error_message_res] %>
        </li>
        <% }); %>
        <% }); %>
      <% }); %>
    </ul>
  </div>

最新更新