'How to get data from a different channel youtube api node.js

I started using YouTube's API today since I want to make a Discord bot that can display a YouTube channels data. So, I went to the guides on the YouTube API site, followed the node.js guide, but ran into a problem. I do not know how I can get the data from a different channel than Google Developers (which is the channel their pulling data from in the explanation).

function getChannel(auth) {
    var service = google.youtube('v3');
    service.channels.list({
      auth: auth,
      part: 'snippet,contentDetails,statistics',
      forUsername: 'GoogleDevelopers'
    }, function(err, response) {
      if (err) {
        console.log('The API returned an error: ' + err);
        return;
      }
      var channels = response.data.items;
      if (channels.length == 0) {
        console.log('No channel found.');
      } else {
        console.log('This channel\'s ID is %s. Its title is \'%s\', and ' +
                    'it has %s views.',
                    channels[0].id,
                    channels[0].snippet.title,
                    channels[0].statistics.viewCount);
      }
    });
  }

Above is the code they use. I expected I could change Google Developers to any other YouTube channel and it would return the data from that, but if I change it to, for example Fireship, I get the error below. I searched their API reference, but I don't really understand what I'm doing wrong.

if (channels.length == 0) {
                   ^

TypeError: Cannot read properties of undefined (reading 'length')

What should I do to fix this issue?

Thanks in advance!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source