My 'errors' variable in ejs template causes 'undefined' error on page load
I have an ejs template for a contact-form with code to output errors via flash messages to the page when the form is not properly or completely filled out.
<%- messages('message', locals) %>
<% if (errors !== undefined) { %>
// Output the errors to the page
<% errors.forEach(function(error) { %>
<div class="alert alert-danger">
<%= error.message %>
</div>
<% }) %>
<% } %>
The code in my app.js file creates the 'errors' variable.
let errors = req.validationResult();
if (errors) {
res.render('contact-form', {
errors: errors
});
For what it's worth, the 'messages.ejs' template contains the following code:
<div id="messages">
<% Object.keys(messages).forEach(function (type) { %>
<div class="alert alert-<%= type %>">
<% messages[type].forEach(function (message) { %>
<p><%= message %></p>
<% }) %>
</div>
<% }) %>
</div>
The line:
<% if (errors !== undefined) { %>
from the contact-form.ejs template is throwing the 'errors is not defined' error.