So I'm relatively new to javascript, and followed this tutorial in API linking, but there's one thing I'm confused about.
Here's my code,
const app = document.getElementById('root');
const logo = document.createElement('img');
logo.src = 'logo.png';
const container = document.createElement('div');
container.setAttribute('class', 'container');
app.appendChild(logo);
app.appendChild(container);
var request = new XMLHttpRequest();
request.open('GET', 'https://ghibliapi.herokuapp.com/films', true);
request.onload = function () {
// Begin accessing JSON data here
var data = JSON.parse(this.response);
if (request.status >= 200 && request.status < 400) {
data.forEach(movie => {
const card = document.createElement('div');
card.setAttribute('class', 'card');
const h1 = document.createElement('h1');
h1.textContent = movie.title;
const p = document.createElement('p');
movie.description = movie.description.substring(0, 300);
p.textContent = `${movie.description}...`;
container.appendChild(card);
card.appendChild(h1);
card.appendChild(p);
});
} else {
const errorMessage = document.createElement('marquee');
errorMessage.textContent = `Gah, it's not working!`;
app.appendChild(errorMessage);
}
}
request.send();
and here's the link to the JSON data querying the API will give you https://ghibliapi.herokuapp.com/films/
So the one thing I'm confused about is this line, and subsequent lines containing movie.something
data.forEach(movie => {
I don't understand why you would use "movie" It's not defined in the code or the actual JSON, so how would you know that it's "movie.description" instead of something like "film.description"? I'm sure that if I can figure this out it's the key to working with other API's and referencing their data.
Can anyone help me?
(Also, here's the actual API documentation, https://ghibliapi.herokuapp.com/#)