如何获取connect-flash以返回多条消息,或一条包含换行符的消息? [英] How do I get connect-flash to return multiple messages, or a single message with newline characters?
问题描述
我正在使用nodejs构建一个应用程序.
I'm building an app using nodejs.
我创建了一个表单,并且正在进行用户输入的后端验证.基本上,我有一个变量,即消息",每次遇到输入错误时,都会将该错误附加到消息中.
I created a form, and I'm working on back-end validation of user input. Basically, I have a var, "messages", and each time I encounter an input error, I append the error to messages.
var messages ="";
errors.forEach(function(msgObject) {
console.log(msgObject.message);
messages += msgObject.message + "\r\n";
})
(我也在使用指示性的 http://indicative.adonisjs.com/- -用于错误验证.它会返回数组错误)
(I'm also using indicative -- http://indicative.adonisjs.com/ -- for error validation. It returns an array errors)
我正在使用connect-flash将错误返回给用户
I'm returning the errors to the user using connect-flash
req.flash("error", messages);
我正在使用连接闪存 https://www.npmjs.com/package/connect-flash
I'm using connect-flash https://www.npmjs.com/package/connect-flash
我的问题是connect-flash会忽略换行符.即,我得到类似的东西:
My problem is that connect-flash ignores newline characters. I.e, I get something like:
我希望每个错误消息都在单独的行中.我似乎找不到解决该问题的方法.有什么想法吗?
I would like each error message to be on a separate line. I can't seem to find a way to accomplish that. Any ideas?
这是问题的一个简单版本:为什么req.flash("errors","hello \ n再见")返回
Here's a simpler version of the problem: Why does req.flash("errors", "hello \n goodbye") return
hello goodbye
代替
hello
goodbye
推荐答案
实际上更好..
EJS文件:
<div class="container">
<% if(error && error.length > 0 ) { %>
<div class="alert alert-danger">
<% if(error.length === 1) { %>
<strong> <%= error %> </strong>
<% } else { %>
<ul>
<% error.forEach(function(err) { %>
<li> <strong> <%= err %> </strong></li>
<% }) %>
</ul>
<% } %>
</div>
<% }
if(success && success.length > 0) { %>
<div class="alert alert-success">
<strong> <%= success %> </strong>
</div>
<% } %>
</div>
.js文件
var messages = [];
errors.forEach(function(msgObject) {
messages.push(msgObject.message);
})
req.flash("error", messages)
这篇关于如何获取connect-flash以返回多条消息,或一条包含换行符的消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!