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 trialZachary Smith
Full Stack JavaScript Techdegree Graduate 16,616 PointsHow on earth do you get the property names in the generateHTML function??
I understand how this code works, but I don't understand where the "title" "description" "extract" names come from. I assume it is from the wikipedia API? If so how can you view those property names?
<img src=${data.thumbnail.source}>
<h2>${data.title}</h2>
<p>${data.description}</p>
<p>${data.extract}</p>
2 Answers
kols
27,007 PointsYou're correct — it's via the Wikipedia API.
If you'd like to explore, you can view more here: https://en.wikipedia.org/api/rest_v1/ (which is the first part of URL linked in the workspace & I believe it was directly linked in the Teacher's Notes section in a previous video, as well).
Colton Shaw
12,634 PointsWe are generating the data from the below functions. In our Event listener, we are calling getJSON
and providing it the information from the AstrosURL. Once we have that successfully returned it feeds that returned 2D array into our Callback function. In this example, our call back function is the anonymous function where we extract the person.name
from each of the objects then send that off to another getJSON
function where we use the WikiURL + name to generate more data from Wikipedia. This is where your question comes in.
// 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', () => {
getJSON(astrosUrl, (json) => {
json.people.map ( person => {
getJSON(wikiUrl + person.name, generateHTML);
});
});
event.target.remove();
});
When Wikipedia returns the information, this is in a JSON format for us to parse. Which is essentially an object with Key/Values. When we call data.title
we are asking for the value of the key title
from what we have defined as data
.
Below is the actual response where you can see how this is formatted.
{
"type":"standard",
"title":"Sergey Kud-Sverchkov",
"displaytitle":"Sergey Kud-Sverchkov",
"namespace":{
"id":0,
"text":""
},
"wikibase_item":"Q26776784",
"titles":{
"canonical":"Sergey_Kud-Sverchkov",
"normalized":"Sergey Kud-Sverchkov",
"display":"Sergey Kud-Sverchkov"
},
"pageid":63560152,
"thumbnail":{
"source":"https://upload.wikimedia.org/wikipedia/commons/thumb/2/2b/Jsc2020e032664.jpg/213px-Jsc2020e032664.jpg",
"width":213,
"height":320
},
"originalimage":{
"source":"https://upload.wikimedia.org/wikipedia/commons/2/2b/Jsc2020e032664.jpg",
"width":1041,
"height":1562
},
"lang":"en",
"dir":"ltr",
"revision":"986210473",
"tid":"f793f870-1ab3-11eb-bccb-bdd2ebfb721e",
"timestamp":"2020-10-30T13:29:34Z",
"description":"Russian engineer and cosmonaut",
"description_source":"local",
"content_urls":{
"desktop":{
"page":"https://en.wikipedia.org/wiki/Sergey_Kud-Sverchkov",
"revisions":"https://en.wikipedia.org/wiki/Sergey_Kud-Sverchkov?action=history",
"edit":"https://en.wikipedia.org/wiki/Sergey_Kud-Sverchkov?action=edit",
"talk":"https://en.wikipedia.org/wiki/Talk:Sergey_Kud-Sverchkov"
},
"mobile":{
"page":"https://en.m.wikipedia.org/wiki/Sergey_Kud-Sverchkov",
"revisions":"https://en.m.wikipedia.org/wiki/Special:History/Sergey_Kud-Sverchkov",
"edit":"https://en.m.wikipedia.org/wiki/Sergey_Kud-Sverchkov?action=edit",
"talk":"https://en.m.wikipedia.org/wiki/Talk:Sergey_Kud-Sverchkov"
}
},
"extract":"Sergey Vladimirovich Kud-Sverchkov was born on August 23, 1983 at the Baikonur Cosmodrome in the Kazakh Soviet Socialist Republic. Sergey Kud-Sverchkov is married and father of one daughter and one son. Since April 2010, he is a Russian Cosmonaut of the Russian Space Agency Roscosmos. He is currently in space.",
"extract_html":"<p><b>Sergey Vladimirovich Kud-Sverchkov</b> was born on August 23, 1983 at the Baikonur Cosmodrome in the Kazakh Soviet Socialist Republic. Sergey Kud-Sverchkov is married and father of one daughter and one son. Since April 2010, he is a Russian Cosmonaut of the Russian Space Agency Roscosmos. He is currently in space.</p>"
}
Zachary Smith
Full Stack JavaScript Techdegree Graduate 16,616 PointsZachary Smith
Full Stack JavaScript Techdegree Graduate 16,616 PointsGot it, thank you!