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 Async Programming and Callback Functions

Thomas Brouns
Thomas Brouns
2,607 Points

getJSON(astrosUrl) appears undefined

I copied the exact code as shown in the video, but my getJSON("astrosUrl") returned as undefined. Is there a way to solve this problem?

Steven Parker
Steven Parker
231,272 Points

We need more information (including the complete code) to replicate the issue. Take a look at this video about Posting a Question, and perhaps also this one about sharing a snapshot of your workspace.

2 Answers

Steven Parker
Steven Parker
231,272 Points

Never post a comment as an "answer", folks looking for unanswered questions to help with may skip right by it!

Also, this code won't run by itself, it needs an HTML component. When you have issues with workspace projects, always make a snapshot of your workspace and post the link to it here.

But the "getJSON" function has no "return" statement to pass back any value, so by definition its return value is "undefined". This function is designed to work asynchronously, so having no return value is typical and normal.

Thomas Brouns
Thomas Brouns
2,607 Points

Hi, sorry about the unclear example, I hope this one makes more sense. I opened the workspace with the following code.

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) {
  const xhr = new XMLHttpRequest();
  xhr.open('GET', url);
  xhr.onload = () => {
    if(xhr.status === 200) {
      let data = JSON.parse(xhr.responseText);
      console.log(data);
    }
  };
  xhr.send();
}

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

getJSON(astrosUrl);

In the video, all the names of the astrosUrl variable get pushed to the console. I only get "undefined" to show up in the console.

Many thanks in advance!