I'm trying to use lodash within an HTML template for an email in Node.js. I have one array with several objects. I would like to iterate through each object and list all of the repeating values. When I use the code below, I receive an error stating that the value is undefined (e.g., ReferenceError: firstName is not defined). The HTML template is in a separate file.
Any thoughts on what I'm doing wrong?
Javascript:
var template = fs.readFileSync('server/views/email-template.html').toString();
var htmlAll = _.template(template)(orderInfo);
HTML:
<% _.forEach(function(firstName) { %><%- firstName %></td><% }); %> <% _.forEach(function(lastName) { %><%- lastName %></td><% }); %>
<% _.forEach(function(address) { %><%- address %></td><% });%>
<% _.forEach(function(city) { %><%- city %><% }); %>, <% _.forEach(function(state.code) { %><%- state.code %><% });
%> <% _.forEach(function(zip) { %><%- zip %><% }); %>
<% _.forEach(function(item) { %><td><%- item %></td><% }); %>
<% _.forEach(function(cost) { %><td><%- cost %></td><% }); %>
Array:
[
{
"firstName": "John",
"lastName": "Doe",
"address": "123 Broadway",
"city": "New York",
"state": {
"code": "NY",
"state": "New York"
},
"zip": "10001",
},
{
"color": "White",
"size": "M",
"item": "T-Shirt",
"cost": 19.99,
},
{
"color": "Blue",
"size": "L",
"item": "T-Shirt",
"cost": 19.99,
}
]