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";
})
(I'm also using indicative -- http://indicative.adonisjs.com/ -- for error validation. It returns an array errors)
I'm returning the errors to the user using connect-flash
req.flash("error", messages);
I'm using connect-flash https://www.npmjs.com/package/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?
Here's a simpler version of the problem: Why does req.flash("errors", "hello \n goodbye") return
hello goodbye
instead of
hello
goodbye
Actually even better..
EJS FILE:
<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 FILE
var messages = [];
errors.forEach(function(msgObject) {
messages.push(msgObject.message);
})
req.flash("error", messages)