For loop not returning enough iterations

Ok, this is weird. I’ve got a for loop that is exiting sooner than I expect. http://codepen.io/AbdiViklas/pen/BzbVgd?editors=0010

I’m working on the Wikipedia Viewer, and have nested a second Ajax call inside my first because I’d really like some images with the results. It all works–but only for some of the results. The for loop in line 25 is adding images to less than all the results. I added the line console.logging i, and for the search term “fox” it only gets up to 11; for “bobsled” it only gets to 1.

Is there something in the results themselves that is causing the loop to abort?

You can see by error in console, that:

Cannot read property ‘source’ of undefined

and your code:

$("#image" + i).append("<img src='" + newData.query.pages[i].thumbnail.source + "'>");

so problem is with thumbnail.

Check last received object, and you’ll see it has no thumbnail property.

1 Like