Code review: session 2

2023. 10. 28. 01:46Javascript Node.js/Project: Personal

It is a personal supplementary lesson.

From now on study in depth is about JS code.

OPEN API

// OPEN API
async function post() {
    const url = `https://api.themoviedb.org/3/movie/upcoming?language=en-US&page=1/`;
    const options = {
        method: "GET",
        headers: {
            accept: "application/json",
            Authorization:
                "Bearer eyJhbGciOiJIUzI1NiJ9.eyJhdWQiOiJhMDkzNzRjY2EyNTc1NjM4ZDEwMDk3NzAzYjFhODliYSIsInN1YiI6IjY1MmZiZWRkYTgwMjM2MDBmZDJkOWY0NiIsInNjb3BlcyI6WyJhcGlfcmVhZCJdLCJ2ZXJzaW9uIjoxfQ.ga70Ew8jOvgDuOUzMiuJgfI8GjGGypablmY74WjMtUs",
        },
    };

    const res = await fetch(url, options);
    const data = res.json();
    if (res.ok) {
        return data;
    } else {
        throw Error(data);
    }
}

I approach the TMDB of OPEN API.

It was never this organization.

const fetch = require('node-fetch');

const url = 'https://api.themoviedb.org/3/movie/top_rated?language=en-US&page=1';
const options = {
  method: 'GET',
  headers: {
    accept: 'application/json',
    Authorization: 'Bearer eyJhbGciOiJIUzI1NiJ9.eyJhdWQiOiJhMDkzNzRjY2EyNTc1NjM4ZDEwMDk3NzAzYjFhODliYSIsInN1YiI6IjY1MmZiZWRkYTgwMjM2MDBmZDJkOWY0NiIsInNjb3BlcyI6WyJhcGlfcmVhZCJdLCJ2ZXJzaW9uIjoxfQ.ga70Ew8jOvgDuOUzMiuJgfI8GjGGypablmY74WjMtUs'
  }
};

fetch(url, options)
  .then(res => res.json())
  .then(json => console.log(json))
  .catch(err => console.error('error:' + err));

Even at this time, I didn't know how to use it.

I never found any data using console.log("fetch");

So, I asked a question for a member of the team.

I got a hint.

It is difficult to read this code in this state.

If you change to the async await function that code is easy to read and is better than currently.

After I changed and used it.

Question: Is there any other way?

You have to remember this.

The "Global" variable

The function is used for the global variable. 

Global variables are used for functions 

Recompose the system

It makes the OPEN API the brain of the web page.

Signals must be sent from the brain's functions so that the eyes, nose, mouth, etc functions can utilize these signals.

The existing function is Okay, but the developer has to develop the code obligate.

 

 

'Javascript Node.js > Project: Personal' 카테고리의 다른 글

Reconstitution: change HTML  (0) 2023.10.31
Code review: session 1  (0) 2023.10.23
Problem: what is API?  (1) 2023.10.19