Not able to display value from array of objects in express node js with EJS template

心已入冬 提交于 2020-07-09 19:38:08

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!