in Education by
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++){ %> image <% } %> <% } %>

<%= 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)

1 Answer

0 votes
by
You're mixing the concepts of arrays and objects. In JS they are not the same thing. Your database should probably be: 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: {} } Then you can access posts simply with dataBase.posts. Here is some more details about arrays vs objects: https://medium.com/@zac_heisey/objects-vs-arrays-42601ff79421#:~:text=Both%20objects%20and%20arrays%20are%20considered%20%E2%80%9Cspecial%E2%80%9D%20in%20JavaScript.,store%20a%20list%20of%20values.

Related questions

0 votes
    So I have this dataBase that is gonna be used in the app.get function below. The goal is to make ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 3, 2022 in Education by JackTerrance
0 votes
    1. Name the term which is used to join two separate arrays into a single array. 2. How are the following ... answer fast I want please Select the correct answer from above options...
asked Dec 13, 2021 in Education by JackTerrance
0 votes
    1. Name the term which is used to join two separate arrays into a single array. 2.How are the following ... ii) primitive type data Select the correct answer from above options...
asked Dec 13, 2021 in Education by JackTerrance
0 votes
    malo 4. Name the term which is used to join two separate arrays into a single array. 5. How are the following passed to the functions? Select the correct answer from above options...
asked Dec 13, 2021 in Education by JackTerrance
0 votes
    Sheet reference uses sheet name,__________, and cell address. Exclamation Mark Operator Dollar Sign Select the correct answer from above options...
asked Nov 26, 2021 in Education by JackTerrance
0 votes
    I have an ArrayList of LongArrays, each of size 2. I am trying to use the built-in 'indexOf' ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    What is indexOf method in Java, I can't seem to find it anywhere. Also how do I use it? Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
0 votes
    When transaction Ti requests a data item currently held by Tj, Ti is allowed to wait only if it ... Answers, Database Interview Questions and Answers for Freshers and Experience...
asked Oct 11, 2021 in Education by JackTerrance
0 votes
    When transaction Ti requests a data item currently held by Tj, Ti is allowed to wait only if it ... Answers, Database Interview Questions and Answers for Freshers and Experience...
asked Oct 11, 2021 in Education by JackTerrance
0 votes
    how to print two arrays side by side in javascript?...
asked Dec 10, 2020 in Technology by JackTerrance
0 votes
    I have some wizard-like pages in my application, its keeping state while navigating between small steps, the ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    Which of the following can’t be tested automatically by a script? 1. CAPTCHA 2. Login Form 3. Button Click 4. Link Navigation...
asked Jul 11, 2021 in Education by JackTerrance
0 votes
    I have an object with an attribute that is a list. For example: obj.a = [3, 4, 5] I ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 3, 2022 in Education by JackTerrance
0 votes
    I have an object with an attribute that is a list. For example: obj.a = [3, 4, 5] I ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 30, 2022 in Education by JackTerrance
...