Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

175
Visualizações
Can't reference array's item by name indexOf()

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:

<article class="content">
    <h1>POSTS:</h1>
    <% posts.forEach(item=>{ %> <!-- ERROR: forEach is not a function -->
        <div>
            <% if (item.stars){ %>
                <% for(let i = 0; i < item.stars; i++){ %>
                    <img src="images/star.pgn"> 
                <% } %> 
            <% } %>
            <h3><%= item.title %></h3>
            <p><%= item.text %></p>
            <br>
        </div>                
    <% }) %>
</article>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

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.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda