Twitch API - Offline Tab Problems

Here is my codepen:

I finished the all and online tab. Now I am working on the offline tab. I thought I would check the stream for null to see if the channel is offline. I wanted to copy the stream name if it was offline and put it in an offlinechannels array. It doesn’t appear that streams[i] is visible in the program though on line 78 (it comes back as undefined when I try to console log it).

I hope I made sense and thanks for any hints/suggestions.


The reason you’re getting undefined is $.getJSON is an async function. That means that the for loop won’t wait your $.getJSON is done getting the json from twitch. That’s why streams[i] inside $.getJSON gives an undefined. If that makes sense.

1 Like

Adding to what @thisiswhale already mentioned, the for loop finishes before the $.getJSON first response comes back. This means that by the time the response comes back from the first $.getJSON request, i = 8 and since streamers array only has 8 elements, streamers[8] refers to an element which does not exist, so you get undefined each time a response comes back from the $.getsJSON request.

1 Like

Thanks @thisiswhale & @RandellDawson. What are my options for remedying this issue? I’m not sure if I should be reading more about callback functions, promises or what :slight_smile:

The simplest way is to use let i instead of var i, so i retains it’s value within the for loop scope.

Also, there is no reason to use JSON.stringify and JSON.parse within the getJSON callback function. The very nature of getJSON is that the response will have a json format. This means your getJSON callback function could simply be:

  $.getJSON(twitchapi, function(json){
      if ( == null){
1 Like

Thanks for your help again. I remember reading about let vs var and I came to the determination that I usually mean let when I use var. I think I use var out of habit (it’s the first thing I used to declare variables). Now I need to re-read my code and understand how it changes things when I use let rather than var.

Thanks. I reviewed the code and I don’t see how it changes anything (it functions but I am not sure why…the let vs var). Can you dumb it down for me?

@RandellDawson Nevermind I get it now (var vs let).

Now the only part I don’t get is:

Why isn’t the logo, link, and status on the same row/line (I used bootstrap). Sorry for the multiple messages.


@RandellDawson Sorry again, I figured it out. Thanks again.

Sorry, I was driving my way back from a trip earlier today and did not have access to internet. Glad you got it figured out.

No problem. Thanks for your help.