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

138
Visualizações
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 Respostas
Responde à pergunta

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 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