when working with ejs and render a file of comments contains username and comment it only renders the username and the comments is not rendering at all
index of comments
const comments = [
{
username:'memo' ,
Comment:'ha ha ha hahaha'
},
{
username:' makrom' ,
Comment:'xy xy xy xyxyxyxyxyxy'
},
{
username:'meno' ,
Comment:'that is so funny'
},
{
username:'mena' ,
Comment:'here is a comment'
}
]
app.get('/comments' , (req , res) => {
res.render('comments/index' , { comments })
})
EJS
<% for (let c of comments){ %>
<li> <%= c.username%> - <%=c.comment%></li>
<% } %>
expected to render the username and its comment but it only render the username
EJS is JavaScript, and JavaScript is case-sensitive.
As comment property is defined as Comment
, you need to use the same form in the template, so not lowercase c.comment
, which is undefined, and why it doesn't render, but the same form c.Comment
(or change comment property to lowercase in comments
array):
<% for (let c of comments){ %>
<li> <%= c.username%> - <%=c.Comment%></li>
<% } %>