javascript - API Thumbnail Image returns TypeError: cannot read "url" of undefined -


i'm using nyt api in order grab thumbnail/image of article. i'm getting cannot read property "url" of undefined error of images. here's screenshot terminal.

screenshot

since said "url" undefined, decided check (in function) whether image source returned undefined:

var prefix = "http:\/\/static01.nyt.com\/"; var src = prefix + data.multimedia[2].url; if(!src || src == undefined) { src = "img/img-nyt.png"; } else { imgsource = src; } console.log(imgsource); 

but i'm not sure why still return undefined because if is, image source should changed img-nyt.png.

here's sample json request:

"results": [     {         "multimedia":[             {                 "url":"http:\/\/static01.nyt.com\/images\/2016\/01\/20\/us\/20michigan-web\/20michigan-web-thumbstandard.jpg",                 "height":75,                 "width":75,             },             {                 "url":"http:\/\/static01.nyt.com\/images\/2016\/01\/20\/us\/20michigan-web\/20michigan-web-thumblarge.jpg",                 "height":150,                 "width":150,             },             {                 "url":"http:\/\/static01.nyt.com\/images\/2016\/01\/20\/us\/20michigan-web\/20michigan-web-articleinline.jpg",                 "height":127,                 "width":190,             }         ]     } ] 

and here's function entire function gets values json:

// makes request , displays news based on results     function searcharticles(term) {         viewing.innerhtml = term;         term = term.replace(/ /g, "+");         searchurl = "http://api.nytimes.com/svc/search/v2/articlesearch.json?q=" + term + "&api-key=" + searchapi;         var prefix = "http:\/\/static01.nyt.com\/";         maindivtoappendto.innerhtml = null;         $.getjson(searchurl, function(api) {             var = 0;             api.response.docs.foreach(function(data) {                 link = data.web_url;                 var src = prefix + data.multimedia[2].url;                 if(!src || src == undefined) { src = "img/img-nyt.png"; }                 else imgsource = src;                 console.log(imgsource);                 cardtitle = (data.headline.main.length > 43) ? data.headline.main.substring(0, 43) + " .." : data.headline.main;                 postedby = "in " + data["section_name"];                 i++;                 createcardelements();             });             numberofposts.innerhtml = + " results";         });     } 

i can't seem figure out what's wrong this. maybe i'm missing obvious. hope can ..

here's entire: sample url at:

http://api.nytimes.com/svc/search/v2/articlesearch.json?q=donald-trump&api-key=sample-key

instead of checking if data.multimedia[0].url == undefined needed check if data.multimedia[0] == undefined.

i still getting error because trying url undefined object , did magic.


Comments

Popular posts from this blog

android - Why am I getting the message 'Youractivity.java is not an activity subclass or alias' -

python - How do I create a list index that loops through integers in another list -

c# - “System.Security.Cryptography.CryptographicException: Keyset does not exist” when reading private key from remote machine -