JavaScript Asynchronous Programming with JavaScript Understanding Promises Handle Multiple Promises with Promise.all

Joshua Ouma
Joshua Ouma
18,556 Points

What is wrong with my code? Someone help

TypeError: Cannot read property 'source' of undefined at promises.js:35 at (<anonymous>) at generateHTML (promises.js:31) Below is my code:

const astrosUrl = '';
const wikiUrl = '';
const peopleList = document.getElementById('people');
const btn = document.querySelector('button');

function getJSON(url) {
  return new Promise((resolve,reject)=>{
    const xhr = new XMLHttpRequest();'GET', url);
    xhr.onload = () => {
      if(xhr.status === 200) {
      let data = JSON.parse(xhr.responseText);
    xhr.onerror = reject(Error("A network error occured"));

function getProfiles(json) {
  const profile = person => {
    return getJSON(wikiUrl +;      
  return Promise.all(profile);

function generateHTML(data) { person =>{
      const section = document.createElement('section');
      section.innerHTML = `
      <img src=${person.thumbnail.source}>

btn.addEventListener('click', (event) => { = "Loading...";
    .catch(err => {
      peopleList.innerHTML = "<h3>Something went wrong</h3>";

2 Answers

Hi Joshua,

The error is caused by 'thumbnail' not being defined. You are referencing a property that doesn't exist.

<img src=${person.thumbnail.source}>

The json you are sourcing from combining your two Urls is returning data - some of which do not contain 'thumbnail' You will have to put in a condition to test to see if the data is defined before trying to use it.

You could use something like this :

if (typeof person.thumbnail !== 'undefined') {
    // is defined
Brendan Moran
Brendan Moran
13,743 Points

To troubleshoot this, use console.log(data) to see what you are dealing with before the error occurs. Put it in the first line of the generateHTML function. You might see that one of the profiles failed to come back from wikipedia. If that's the case, look at some of the other questions and answers on this video that deal with that problem.