Twitch API - Offline Tab Problems

Here is my codepen:
TWITCH API 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.

J

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

Thanks @thisiswhale & @camperextraordinaire. 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 (json.stream == null){
         console.log(streams[i]);
      }
   });
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?

@camperextraordinaire 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.

Thanks.

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

No problem. Thanks for your help.