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 trial

JavaScript Asynchronous Programming with JavaScript Asynchronous JavaScript with Callbacks Implement a Callback

Cannot read property "source" of undefined

If an astronaut doesn't have a profile image, the callback fails for that instance. While there currently are three astronauts in space, the first one doesn't have an image and therefore doesn't appear.

rydavim
rydavim
18,814 Points

You'll need to post your code in order for the community to help you troubleshoot. The easiest way to do this is by posting a Workspace Snapshot. You can do this using the camera icon at the top of the workspace window, or review the video I linked for more information.

James Shore
James Shore
3,709 Points

I think its because the name we are being sent by the astrourl is a different name to what wikipedia is storing

3 Answers

Bobby Verlaan
Bobby Verlaan
29,538 Points

There are now other astronauts in space compared to when the teacher made the video, so we are requesting different URLs with the API of Wikipedia to get the astronaut articles.

At this moment (April 2020) I get back 6 astronauts from open-notify.org. Looking at the data I get back from the Wikipedia API, I see 5 of the 6 requests for the astronauts give a 200 response. One fails with a 404 - not found. From the 5 responses I get back from Wikipedia you get an array output like this:

0: {type: "disambiguation", title: "Andrew Morgan", displaytitle: "Andrew Morgan", namespace: {…}, wikibase_item: "Q16263257", …}
1: {type: "standard", title: "Oleg Skripochka", displaytitle: "Oleg Skripochka", namespace: {…}, wikibase_item: "Q927249", …}
2: {type: "no-extract", title: "Chris Cassidy", displaytitle: "Chris Cassidy", namespace: {…}, titles: {…}, …}
3: {type: "standard", title: "Ivan Vagner", displaytitle: "Ivan Vagner", namespace: {…}, wikibase_item: "Q21856852", …}
4: {type: "standard", title: "Jessica Meir", displaytitle: "Jessica Meir", namespace: {…}, wikibase_item: "Q4795114", …}

Only 3 of these responses give back the article of the type 'standard'. Only in that type of response there is a thumbnail.source available. Therefore, only those three get rendered to my page.

I assume you have something similar going on. Hope this helps :-)

Viktor Lovgren
Viktor Lovgren
6,241 Points

There is surely a better way to fix this that can apply to more cases, but right now these names need fixing: Chris Cassidy, Anatoly Ivanishin

btn.addEventListener("click", () => {
  getJSON(astrosUrl, (json) => {
    json.people.map((person) => {
      if (person.name === "Anatoly Ivanishin") {
        person.name = "Anatoli Ivanishin";
      }
      if (person.name === "Chris Cassidy") {
        person.name = "Christopher Cassidy";
      }
      getJSON(wikiUrl + person.name, generateHTML);
    });
  });
});
Alexey Serpuhovitov
Alexey Serpuhovitov
6,931 Points

I fixed it with a dictionary of names.

const imena = {
  'Anatoly': 'Anatoli',
  'Chris': 'Christopher'
};


function getProfile(json) {

  const profiles = json.people.map( (person) => {
    let firstName = person.name.split(' ')[0]; // find the first name

    if (imena[firstName] != undefined) { // if the first name of an astronaut is in the dictionary, replace it 
      person.name = person.name.replace(firstName, imena[firstName]);
    }
    return getJSON(wikiUrl + person.name);
  });
  return Promise.all( profiles );
}