I am using OMDb API to build a movie search platform for some practise in VS Code. I was watching a video and my syntax is almost identical, I am successfully pulling the data in a promise. Now when I go to use .map()
to change the innerHTML, I’m pulling an error.
async function main() {
const movie = await fetch("https://www.omdbapi.com/?i=tt3896198&apikey=ba040381");
const movieData = await movie.json();
const movieListEl = document.querySelector(".movie__list");
console.log(movieData) // to prove data is being pulled
movieListEl.innerHTML = movieData
.map(
(movie) =>
`<div class="movie__card">
<div class="movie__card--container">
<h3>${movie.title}</h3>
<p><b>Year:</b>0000</p>
<p><b>Rating:</b>xxxxx</p>
<p><b>Runtime:</b>xxxxx</p>
<p><b>Genre:</b>xxxxx</p>
</div>
</div>`
)
.join("");
}
main();
This is not perfect by any means, but in the video this syntax seemed to work perfectly before cleaning up the code and making it easier to read by creating a separate function to change the HTML
I’m very lost and had reached out for some help in this discord channel I’m in with unfortunately no luck. I am very new to JavaScript—and using APIs for that matter. Any feedback is very much appreciated.
2
Answers
You are getting error because
movieData
is an object and you cannot usemap()
method on the object in JavaScript. IfmovieData
is anArray
then you can usemap()
method to iterate over the array elements.The response is not array, response is only one object and you don’t need to iterate, you need distructure object or accees object values directy.
Or
Then show result