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

Elias Guderian
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Elias Guderian
Front End Web Development Techdegree Graduate 14,994 Points

getJSON(...).then(...).then(...).chatch is not a function at HTMLButtonElement.<anonymous>

Hi there, I am following right now Guils video in the 'Asynchronous Programming with JavaScript' course and I followed his instructions exactly but I still get the error listed in the title. The eventlistener still works but it stops at the chatch() method and throws this error also it throws another error which I think is probably related to the one in the title,

'Uncaught (in promise) TypeError: Cannot read property 'source' of undefined at promises.js:36 at Array.map (<anonymous>) at generateHTML'

Maybe I got something wrong or something in the wiki link changed, could someone have a look at my code please?

const astrosUrl = 'http://api.open-notify.org/astros.json';
const wikiUrl = 'https://en.wikipedia.org/api/rest_v1/page/summary/';
const peopleList = document.getElementById('people');
const btn = document.querySelector('button');

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

}

function getProfiles(json) {
  const profiles = json.people.map( person => {
    return getJSON(wikiUrl + person.name);      
  }); 
  return Promise.all(profiles);
}

function generateHTML(data) {
  data.map(person => {
    const section = document.createElement('section');
  peopleList.appendChild(section);
  section.innerHTML = `
    <img src=${person.thumbnail.source}>
    <h2>${person.title}</h2>
    <p>${person.description}</p>
    <p>${person.extract}</p>
  `;
  });

}

btn.addEventListener('click', (event) => {
  event.target.textContent = "Loading...";
  getJSON(astrosUrl)
    .then(getProfiles)
    .then(generateHTML)
    .chatch(err => {
      peopleList.innerHTML = '<h3>Something went wrong!</h3>';
      console.log(err);
    } )
    .finally(() => event.target.remove())
});

1 Answer

It should be catch not chatch. That is the cause of your first error.

This page may help with your second error