Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript Asynchronous Programming with JavaScript Asynchronous JavaScript with Callbacks Async Programming and Callback Functions

Thomas Brouns
Thomas Brouns
2,504 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
218,692 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

Thomas Brouns
Thomas Brouns
2,504 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!

Steven Parker
Steven Parker
218,692 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.