Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

130
Vistas
JS: problem with for loop (only one image is shown)

I am completely new to javascript and web development.

I'm having a problem with the for loop; what happens to me is that the ids come back to me all together and not individually in order to retrieve the relative image and title through the id, so I can't recover images and title from the json array.

Specifically I get the error:

Uncaught TypeError: Cannot read properties of undefined (reading 'immagine') at XMLHttpRequest.xmlhttp.onreadystatechange

Warning: I have no problems in how to retrieve items in a json array because I know how to do it very well

This is my code:

//here I get all the articles, so my json array
var xmlhttp = new XMLHttpRequest();
var url = "https://wjko5u1234.execute-api.us-east-2.amazonaws.com/articles"; 
xmlhttp.onreadystatechange = function() {
  if (this.readyState == 4 && this.status == 200) {
   var allart = JSON.parse(this.responseText);
   var container=document.getElementById("slideshow")
    for(var i = 0; i < allart.Items.length; i++)
    {
      container.innerHTML += '<div class="slideshow-container"></div>';
      document.getElementById("id").innerHTML += "<br/>" + allart.Items[i].id;
      myFunction1(allart.Items[i].id);
    }
  }
};
xmlhttp.open("GET", url, true);
xmlhttp.send();

//here I pass the id via function call, and for each id I want to retrieve image and title which has that specific id only i get the set of ids without having one at a time to retrieve what i need
function myFunction1(id) {
  var xmlhttp = new XMLHttpRequest();
var url = "https://wjko5u1234.execute-api.us-east-2.amazonaws.com/articles/"+id; 
xmlhttp.onreadystatechange = function() {
  if (this.readyState == 4 && this.status == 200) {
   var myArr = JSON.parse(this.responseText);
   console.log(myArr);
   document.getElementById("img1").src="articoli_img/"+myArr.Item.immagine;
   document.getElementById("title1").innerHTML = myArr.Item.titolo;
  }
};
xmlhttp.open("GET", url, true);
xmlhttp.send();
}

I would be very thankful for any help.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

It seems that "Item" prop doesn't exist in returned object (located in myFunction1()) (did you mean to write myArr.immagine?)

Since you are new: I recommend to use for-each loops in collections, by using:

for(let item of allart.Items) {
    .....
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda