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

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

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