S
S
Shura01022021-10-26 15:40:30
API
Shura0102, 2021-10-26 15:40:30

How to get information by api?

Hello! Please help me figure out api. I do it like this:

const API_KEY = "a1979074-3a11-448a-b2d2-55d1bb97cdf4";
const API_URL_POPULAR =
  "https://kinopoiskapiunofficial.tech/api/v2.2/films/1130869";

getMovies(API_URL_POPULAR);

async function getMovies(url) {
  const resp = await fetch(url, {
    headers: {
      "Content-Type": "application/json",
      "X-API-KEY": API_KEY,
    },
  });
  const respData = await resp.json();
  showMovies(respData);
  console.log(respData);
}

function showMovies(data) {
  const moviesEl = document.querySelector(".movies");

  document.querySelector(".movies").innerHTML = "";

  data.forEach((movie) => {
    const movieEl = document.createElement("div");
    movieEl.classList.add("movie");
    movieEl.innerHTML = `
<span>${movie.nameRu}</span>
        `;
    moviesEl.appendChild(movieEl);
  });
}


gives an error in console - Uncaught (in promise) TypeError: data.forEach is not a function

Taking information from another array using the same method, everything works fine.

const API_KEY = "a1979074-3a11-448a-b2d2-55d1bb97cdf4";
const API_URL_POPULAR =
  "https://kinopoiskapiunofficial.tech/api/v2.2/films/1130869";

getMovies(API_URL_POPULAR);

async function getMovies(url) {
  const resp = await fetch(url, {
    headers: {
      "Content-Type": "application/json",
      "X-API-KEY": API_KEY,
    },
  });
  const respData = await resp.json();
  showMovies(respData);
  console.log(respData);
}

function showMovies(data) {
  const moviesEl = document.querySelector(".movies");

  // Очищаем предыдущие фильмы
  document.querySelector(".movies").innerHTML = "";

  data.genres.forEach((movie) => {
    const movieEl = document.createElement("div");
    movieEl.classList.add("movie");
    movieEl.innerHTML = `
<span>${movie.genre}</span>
        `;
    moviesEl.appendChild(movieEl);
  });
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
Chipekwe, 2021-10-26
@Shura0102

data is an object, not an array, so it doesn't have a method.forEach()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question