Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialRebecca Palumbo
5,506 PointsThumbnail missing, so how do I insert a different image for the missing image?
Okay, so for the second astronaut, there is no thumbnail image. Only the first astronaut loads. I can delete the <img src=${person.thumbnail.source}> from the generateHTML function and it'll work without pictures, but I was wondering if there's a way to skip over missing data instead of it crashing the rest of the page, or putting in a no picture image if there's no picture. How would I do this? I can't figure it out. My code looks like what Guil did, but here's a snapshot if you need it: https://w.trhou.se/lyufc4hqb4.
1 Answer
Joseph Turner
Full Stack JavaScript Techdegree Graduate 16,794 PointsI found that my "Andrew Morgan" astronaut was missing the "thumbnail". You can check the api resource to see "thumbnail is missing" : https://en.wikipedia.org/api/rest_v1/page/summary/Andrew_Morgan
So I did an if statement as Agustin suggested:
if(typeof(person.thumbnail) != 'undefined') {
section.innerHTML =
<img src=${person.thumbnail.source}>
;
}
section.innerHTML +=
<span>${person.craft}</span>
<h2>${person.title}</h2>
<p>${person.description}</p>
<p>${person.extract}</p>
;
Agustin Mouratoglou
6,507 PointsAgustin Mouratoglou
6,507 Pointsyou could use an if statement checking if the response object has an image property or not, if not, show a default image.