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 Asynchronous JavaScript with Callbacks Implement a Callback

Cheryl Oliver
Cheryl Oliver
21,676 Points

Cant get the files to work in MAMP

I am trying to get this work in MAMP but none of the changes I make seem to do anything.

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

// Make an AJAX request
function getJSON(url, callback) {
  const xhr = new XMLHttpRequest();
  xhr.open('GET', url);
  xhr.onload = () => {
    if(xhr.status === 200) {
      let data = JSON.parse(xhr.responseText);
      return callback(data);
    }
  };
  xhr.send();
}

// Generate the markup for each profile
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) => {
  console.log("data");
  getJSON(astrosUrl, (json) => {
    json.people.map( person => {
      getJSON(wikiUrl + person.name, generateHTML);

    });
  });
  event.target.remove();
});

So you can see that I have put a console.log("data") in the addeventlistener function but nothing is appearing in the console. The astronauts are showing up on the page, 4 out of the 6 are working and then I am getting an error for 2 saying that the data.thumbnail.source is undefined.

Am I doing something silly? I am very confused!

1 Answer

charlie g
charlie g
12,417 Points

Hi Cheryl! Check this, I had the same problem.

Cheryl Oliver
Cheryl Oliver
21,676 Points

Thanks Charlie, i had a look through those answers and picked the best solution for me. The mamp issue I was having was just a caching problem luckily so thats all fixed now as well :)