You are using the counter i
to select the specific properties of posts
and photos
, which is why it returns undefined, for example:
i = 1;
var getImages = results.response.posts[1].photos[1].alt_sizes[0].url;
posts[1]
is the second post in the response, but photos[1]
refers to the second photo of posts[1]
and probably doesn't exist as its a single photo post.
You need to create another loop to check for more than one photo per post, photosets will have up to ten photos.
var i = 0;
while (i < 50) {
var type = results.response.posts[i].type;
if (type == "photo") {
var photos = results.response.posts[i].photos;
for (var j = 0; j < photos.length; j++) {
console.log( photos[j].alt_sizes[0].url );
}
}
i++;
}
Its also worth mentioning that on older images, if there is no high resolution version available, it doesn't fall back to another URL, its left blank, so you may need to check for that as well.