JavaScript Asynchronous Programming with JavaScript Understanding Promises From Callbacks to Promises

Receiving "Error: Not Found" in console

I cannot find the error in my code. When I view it in the console, I get "Error: Not Found" and "promises.js:15:17".

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) );   //This is line 15 in my workspace
      }
    };
    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 profiles;
}

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

btn.addEventListener('click', (event) => {
  getJSON(astrosUrl)
    .then(getProfiles)
    .then( data => console.log(data) )
    .catch( err => console.log(err) )
  event.target.remove();
});

2 Answers

Steven Parker
Steven Parker
201,932 Points

It might not be your code. These two API's don't agree on the spelling of some of the astronaut's names.

In some previous questions like this one, students have posted some suggested code to compensate for the differences.

Thank you! I was finally able to figure it out from the previous questions that you suggested that I check out.