@rodgi

"UnhandledPromiseRejectionWarning: TypeError: Cannot read property '0' of undefined",что делать?

Код для получения информации о количестве подписчиков на Ютубе. Код:
const { api_key, channel_id } = "./config.json"
const fetch = require('node-fetch')
const livesubs = () => {
    let response = fetch(`https://www.googleapis.com/youtube/v3/channels?part=statistics&id=${channel_id}&key=${api_key}`)
        .then(response => response.json())
        .then(function (response) {
            updateView(response.items[0].statistics.subscriberCount);
        })};
     const updateView = (subscribersCount) => {
        counterValue.innerHTML = subscribersCount.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ".");
    };
    function app() {
        livesubs();
        window.setInterval(livesubs, 5000);
     }

Ошибка: UnhandledPromiseRejectionWarning: TypeError: Cannot read property '0' of undefined
  • Вопрос задан
  • 438 просмотров
Пригласить эксперта
Ответы на вопрос 1
@mitya_k
Добавить catch
.then(function (response) {
            updateView(response.items[0].statistics.subscriberCount);
        })..catch(err => console.log(err))


А вообще вы видимо обращаетесь к структуре, которая не не является массивом. Сделайте console.log(response.items)
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы