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

Console is throwing error for the original code that the teacher provided.

When I run my code, the console throws an error for the original code the teacher provided. It says Uncaught TypeError: Cannot read property 'source' of undefined at generateHTML (callbacks.js:24) at XMLHttpRequest.xhr.onload (callbacks.js:13)

My workspace is https://w.trhou.se/bt44d6t918

1 Answer

Ken Stone
Ken Stone
29,703 Points

You need to check that data.thumbnail.source is indeed set:

// Generate the markup for each profile
function generateHTML(data) {
  const img_src = (data.thumbnail && data.thumbnail.source) ? data.thumbnail.source : '';
  const section = document.createElement('section');
  peopleList.appendChild(section);
  section.innerHTML = `
    <img src=${img_src}>
    <h2>${data.title}</h2>
    <p>${data.description}</p>
    <p>${data.extract}</p>
  `;
}

Okay, thanks! It worked.