Issue
I have a form submitting to an express app:
app.post('/myform', function (req, res) {
var content = new registration(req.body);
content.save(function(errObj){
if(errObj){
res.render('registration',{error: errObj});
} else {
res.render('thankyou');
}
});
If there are any errors in saving the form data to mongoDb I want to display those errors at the top of the form so I’m passing the error object back to the ejs template:
<% if (error) { %>
<li><%= error %></li>
<% } %>
But when I initially load the page and there are no errors yet (because the form has not been submitted) I’m receiving an error on the page and the form is not rendered:
error is not defined
How can I get around this?
Solution
You could use a value in the error property that won’t get through into the if:
res.render('registration', {error: false});
Answered By – Amit
This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0