So I have this dataBase that is gonna be used in the app.get function below. The goal is to make the app.get send the first item in the array dataBase (which is the array "posts") as the value for the object's atribute "posts:". In case I don't set any names for the array and call it in the app.get as "posts: dataBase[0]", it works - the forEach() can read it; otherwise, it seems it's not called as an array since the .forEach() can't be used.
const dataBase =
[
posts = [
{
title: "Post 1",
text: "Lorem ipsum",
stars: 2
},
{
title: "Post 2",
text: "Lorem ipsum"
},
{
title: "Post 3",
text: "Lorem ipsum",
stars: 5
}
],
ads = {}
]
app.get("/posts", (req, res)=>{
res.render("posts",
{
title: "Basic Project: Posts",
posts: dataBase[dataBase.indexOf("posts")]
}
)
})
IN THE .ejs FILE:
POSTS:
<% posts.forEach(item=>{ %> <!-- ERROR: forEach is not a function -->
<% if (item.stars){ %>
<% for(let i = 0; i < item.stars; i++){ %>
<% } %>
<% } %>
<%= item.title %>
<%= item.text %>
<% }) %>
JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)