Need help with the FCC Twitch API project

Dear all,

I have a problem with my pen (https://codepen.io/RJP/pen/KvWGqL). Now I know it currently looks like shit and maybe the way I’m handling the JS is not optimal, but there’s some other issue I’d like help with.

In the JS, I loop through the array “streamers”, but somehow this returns undefined values (if you look at the console log). If I use streamers[0] etc it’s fine though. Does somebody know how this is possible? I’d like to add that yesterday everything still worked fine, and I honestly don’t believe I changed anything, but I might be overlooking someting/be mistaken.

Any help would be very much appreciated!

RJP

in your loop instead of starting with (i=0) use (let i =0). I cant find the part of your code that makes this required at the moment, but it works,

1 Like

Wow, okay. No idea why it should be like this but thanks a lot anyway! :slight_smile:

No problem. I think its because ajax requests are asynchronous, and you go on to use “i” in the success functions. the success functions are only processed when the request is done, and at that point i isnt the value you expected (why that makes them undefined is what I’m too tired to tell :stuck_out_tongue: I cant sleep).

basically since the loop will probably be done before any of the functions in your getJSONs can run (since those functions need to wait for the api response first) you have to use “let” which makes each loop iteration get its own distinct i variable

Good luck!

1 Like