Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

183
Views
No se puede hacer referencia al elemento de la matriz por nombre indexOf()

Así que tengo esta base de datos que se usará en la función app.get a continuación. El objetivo es hacer que app.get envíe el primer elemento en la matriz de base de datos (que es la matriz "publicaciones") como el valor del atributo del objeto "publicaciones:". En caso de que no establezca ningún nombre para la matriz y la llame en la aplicación. obtenga como "publicaciones: base de datos [0]", funciona: forEach () puede leerlo; de lo contrario, parece que no se llama como una matriz ya que no se puede usar .forEach().

 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")] } ) })

EN EL ARCHIVO .ejs:

 <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 answers
Answer question

0

Estás mezclando los conceptos de arreglos y objetos. En JS no son lo mismo.

Su base de datos probablemente debería ser:

 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: {} }

Luego puede acceder a las publicaciones simplemente con dataBase.posts .

Aquí hay más detalles sobre matrices frente a objetos: https://medium.com/@zac_heisey/objects-vs-arrays-42601ff79421 .

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!