问题
I want to display data which is coming from database. In controller file I am getting array of objects and I am passing that array of objects to my ejs file. On my ejs file I am getting that "nameOfArray" is not defined.
Here is my controller file:
product.find().lean().exec((err, products) => {
if(err) {
return;
}
else {
let productsList = products;
res.render('admin/products', { data: productsList });
}
})
Here is product.ejs file:
<table>
<tbody>
<% data.forEach(function(product) { %>
<tr>
<td>
<%= product.productName %>
</td>
</tr>
<% }); %>
</tbody>
</table>
By running this it is showing that data is not defined.
来源:https://stackoverflow.com/questions/61918890/not-able-to-display-value-from-array-of-objects-in-express-node-js-with-ejs-temp