Ejs using forEach in include file, parameter not defined

Viewed 23

When using forEach in the include file, no parameters are defined.
Why is the error resolved when I use "if"?

main.ejs

<% const mainList = [
  {
    term: 'title1',
    description: 'text1'
  },
  {
    term: 'title2',
    description: 'text2'
  }
] %>

<%- include('./sub', { mainList: mainList }) %>

sub.ejs

<% mainList.forEach((item) => { %>
  <dl>
    <dt><%= item.term %></dt>
    <dd><%= item.description %></dd>
  </dl>
<% }) %>

Why is the error resolved?

<% if (typeof mainList !== 'undefined') { %> // Why is the error resolved?
  <% mainList.forEach((item) => { %>
    <dl>
      <dt><%= item.term %></dt>
      <dd><%= item.description %></dd>
    </dl>
  <% }) %>
<% } %>
0 Answers
Related