Friday, 15 February 2013

javascript - The comments I am retrieving when using the Instagram API are returning 'undefined' -



javascript - The comments I am retrieving when using the Instagram API are returning 'undefined' -

i'm retrieving info

https://api.instagram.com/v1/tags/hashtag/media/recent?client_id=client_id

wherehashtag = hashtag i'm searchingclient_id = client idi having no problem retrieving image urls , captions when seek comments comments beingness returned 'undefined'.the info stored in array called pictures if want standard resolution url do:

for(i = 0; < pictures.length; i++){ pictureurl[i] = pictures[i].images.standard_resolution.url; }

right code i'm trying utilize retrieve comments :

//where 'i' index of pic i'm focusing on (comment in pictures[i].comments.data) { alert(comment.text); //using alert see beingness retrieved }

but issue alerts displaying 'undefined' , displaying undefined when there comment (i checked on phone, if pic has no comment, there no alert. if image has comments there 1 alert each comment.can please help me out?

the value in pictures[i].comments.data array, shown in "response" section in /tags/tag-name/media/recent instagram api docs:

[{ "type": "image", ... "comments": { "data": [{ "created_time": "1296703540", "text": "snow", ... }, { "created_time": "1296707889", "text": "#snow", ... }], ...

as can see, data property in comments object array (beginning [).

you're misusing for..in on array. (see why using "for...in" array iteration such bad idea?) for..in loops on property names of objects. value of comment property name string, has no text property.

instead, need plain for loop, because pictures[i].comments.data array:

for (var j=0; j<pictures[i].comments.data.length; j++) { var comment = pictures[i].comments.data[j]; alert(comment.text) }

one of import note if pictures[i].comments.data had been non-array object, utilize of for..in still wouldn't quite right. variable comment holds property names, , need utilize property-access value property name refers to:

for (commentkey in pictures[i].comments.data) { var commentvalue = pictures[i].comments.data[commentkey]; alert(commentvalue.text) }

note might work arrays, but:

the property names may not loop in numerical order this loop on all iterable properies of array, not numeric indices

javascript jquery instagram

No comments:

Post a Comment