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 Exploring Async/Await Convert Promise Handling to Async/Await

Amanda Richardson
seal-mask
.a{fill-rule:evenodd;}techdegree
Amanda Richardson
Web Development Techdegree Student 13,776 Points

Error - can't find profileResponse variable??

I'm getting an error on this async/await code of an "Unhandled Promise Rejection: ReferenceError: Can't find variable: profileReponse"

I've tried to match my code to Guil's but no success. It's probably a stupid error but what am I doing wrong???

Also - as a sidenote - these lectures really should be re-done. They're hard to follow and too many broken links.

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');

// Handle all fetch requests
async function getPeopleInSpace(url) {
  const peopleResponse = await fetch(url);
  const peopleJSON = await peopleResponse.json();

  const profiles = peopleJSON.people.map(async (person) => {
     const craft = person.craft;                                  
    const profileResponse = await fetch(wikiUrl + person.name);
    const profileJSON = await profileReponse.json();

    return {...profileJSON, craft};
  });

return Promise.all(profiles);
}

// Generate the markup for each profile
function generateHTML(data) {
  data.map( person => {
    const section = document.createElement('section');
    peopleList.appendChild(section);
    // Check if request returns a 'standard' page from Wiki
    if (person.type === 'standard') {
      section.innerHTML = `
        <img src=${person.thumbnail.source}>
        <span>${person.craft}</span>
        <h2>${person.title}</h2>
        <p>${person.description}</p>
        <p>${person.extract}</p>
      `;
    } else {
      section.innerHTML = `
        <img src="img/profile.jpg" alt="ocean clouds seen from space">
        <h2>${person.title}</h2>
        <p>Results unavailable for ${person.title}</p>
        ${person.extract_html}
      `;
    }
  });
}

btn.addEventListener('click', async (event) => {
  event.target.textContent = "Loading...";

  const astros = await getPeopleInSpace(astrosUrl);
  generateHTML(astros);
  event.target.remove();


});

1 Answer

    const profileResponse = await fetch(wikiUrl + person.name);
    const profileJSON = await profileReponse.json();

Try changing profileReponse.json(); to profileResponse.json();