Geek Logbook

Tech sea log book

50projectsIn50days – Day 17: Movie App

The movie app needs you create an account in the movie db beacuse you need this access to get the data


const API_URL = ''+API_KEY+'&page=1'
const IMG_PATH = ''
const SEARCH_API = ''+API_KEY+'&query="'

The new part in this path is the oner related to take information from another services, in this case a movie information provider. This information is obtained using the function getMovies(url)


async function getMovies(url) {
    const res = await fetch(url)
    const data = await res.json()


Using the fetch API is at the beginign challeging because this not return us data we can consume. We have to do something this. In fact, the fetch API return a response. As we can see in the documentation:

The Response object, in turn, does not directly contain the actual JSON response body but is instead a representation of the entire HTTP response.

Using Fetch

To extract the data the documentation tell us we could use the json method:

So, to extract the JSON body content from the Response object, we use the json() method, which returns a second promise that resolves with the result of parsing the response body text as JSON.

Using Fetch

The fech API resturn a Promise:

The fetch() method takes one mandatory argument, the path to the resource you want to fetch. It returns a Promise that resolves to the Response to that request — as soon as the server responds with headers — even if the server response is an HTTP error status

Fetch API

And for using the promise we also have to understand that we need to use the ‘async’ keyword. Fetch is an asynchronous operation wich may take some time to complete.

Promise is an object representing the eventual completion or failure of an asynchronous operation. Since most people are consumers of already-created promises, this guide will explain consumption of returned promises before explaining how to create them.

Using promises

After finishing the new part, which is retrieve the promise, know the request an load the information in a variable we can load the information in the innerHTML:

function showMovies(movies){
    main.innerHTML = ''

    movies.forEach((movie) => {
        const {title, poster_path, vote_average, overview} = movie

        const movieEl = document.createElement('div')

        movieEl.innerHTML = `
        <img src="${IMG_PATH + poster_path}" alt="${title}"/>
        <div class="movie-info">
            <span class="${getClassByRate(vote_average)}">${vote_average}</span>
        <div class="overview">

You can see the code in this GitHub Repository: 17.movie_app and the demo of the project is here: Movie App

Leave a Reply

Your email address will not be published. Required fields are marked *.

You may use these <abbr title="HyperText Markup Language">HTML</abbr> tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>