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 trialGavin Eyquem
Front End Web Development Techdegree Student 20,735 PointsJSON.people is undefined. How do I fix this?
I've followed the steps and found the error JSON.people when running the code and looking in the console.
How do I fix this?
Gavin Eyquem
Front End Web Development Techdegree Student 20,735 PointsHere's my 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, 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);
// 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}
;
}
}
btn.addEventListener('click', () => { getJSON(astrosUrl, (json) => { JSON.people.map(person => { getJSON(wikiUrl + person.name, generateHTML); }); }); });
Steven Parker
231,271 PointsA snapshot allows your entire working environment to be replicated, which will include the HTML part which your program needs to run. Please use that whenever asking about workspace code.
And when posting non-workspace code, at least be sure to use Markdown formatting to preserve the code's appearance.
Gavin Eyquem
Front End Web Development Techdegree Student 20,735 PointsSteven Parker...thankyou for the lower case json suggestion. It worked.
1 Answer
Steven Parker
231,271 PointsEven without being able to try the code, "JSON" is a built-in object that has no "people" attribute. Did you perhaps mean to reference the parameter "json" (lower case) instead?
Steven Parker
231,271 PointsSteven Parker
231,271 PointsIt will be easier to help if we can see your code. You might want to take a look at this video about Posting a Question. And for when your question refers to something you are doing in a workspace, you may also want to view this one about sharing a snapshot of your workspace.