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

Using fetch API

I don't understand why the code gives me error when I remove the comment from the catch api.

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 getProfiles(json) { const profiles = json.people.map(person => { return fetch(wikiUrl + person.name) .then(respose => respose.json()) }); 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....';

fetch(astrosUrl)
    .then(response => response.json())
    .then(getProfiles)
    .then(generateHTML)
/*  .catch(err => {
        peopleList.innerHTML = '<h3>Something went wrong!</h3>';
    })*/
    .finally(() => event.target.remove())
;

});

1 Answer

What error are you getting?