# Twitch Tv - hooking up name and stream

**URL:** <https://forum.freecodecamp.org/t/twitch-tv-hooking-up-name-and-stream/67520>\
**Category:** General\
**Created:** [December 20, 2016, 4:37pm UTC](https://forum.freecodecamp.org/t/twitch-tv-hooking-up-name-and-stream/67520 "2016-12-20T16:37:02Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![timotheap](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/timotheap/32/26039_2.png) [@timotheap](https://forum.freecodecamp.org/u/timotheap)\
**Post date:** [December 20, 2016, 4:37pm UTC](https://forum.freecodecamp.org/t/twitch-tv-hooking-up-name-and-stream/67520/1 "2016-12-20T16:37:02Z")

</div>

It really bugs me that I can’t find the solution - I’m not even sure where to look.

I can have the offline/online status but it doesn’t iterate through all users, which means I get the same status for everybody (i.e. online OR offline).

Another weird thing is that sometimes it will be ALL offline, sometimes ALL offline.

I suppose it has to do that the offline/online status in my function isn’t linked to a particular user. I’ve tried to solve that (for loops, nested Ajax, …) but to no avail.

Can someone show me in the right direction? Here’s my [pen] ([http://s.codepen.io/timotheap/debug/PbLWmW](http://s.codepen.io/timotheap/debug/PbLWmW)). - the CSS is just to see what’s going on.

---

<div class="post-metadata">

**Author:** ![Oxyrus](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/oxyrus/32/69509_2.png) [@Oxyrus](https://forum.freecodecamp.org/u/Oxyrus)\
**Post date:** [December 20, 2016, 4:41pm UTC](https://forum.freecodecamp.org/t/twitch-tv-hooking-up-name-and-stream/67520/2 "2016-12-20T16:41:24Z")

</div>

Where is the code? the link to GitHub doesn’t work.

---

<div class="post-metadata">

**Author:** ![timotheap](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/timotheap/32/26039_2.png) [@timotheap](https://forum.freecodecamp.org/u/timotheap)\
**Post date:** [December 20, 2016, 4:42pm UTC](https://forum.freecodecamp.org/t/twitch-tv-hooking-up-name-and-stream/67520/3 "2016-12-20T16:42:38Z")

</div>

I haven’t put it on Github yet, that’s why I put everything on Codepen, sorry for the confusion.

---

<div class="post-metadata">

**Author:** ![Oxyrus](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/oxyrus/32/69509_2.png) [@Oxyrus](https://forum.freecodecamp.org/u/Oxyrus)\
**Post date:** [December 20, 2016, 4:47pm UTC](https://forum.freecodecamp.org/t/twitch-tv-hooking-up-name-and-stream/67520/4 "2016-12-20T16:47:32Z")

</div>

Could you add a `console.log` with the returned data? (the array of objects)

Edit: I’ve added it myself, however an object looks like this:

```auto
Object
_id
:
30220059
_links
:
Object
bio
:
"For standings, schedule, and results, visit http://www.intelextrememasters.com/"
created_at
:
"2012-05-02T09:59:20Z"
display_name
:
"ESL_SC2"
logo
:
"https://static-cdn.jtvnw.net/jtv_user_pictures/esl_sc2-profile_image-d6db9488cec97125-300x300.jpeg"
name
:
"esl_sc2"
type
:
"user"
updated_at
:
"2016-12-20T16:04:27Z"

```

Where is the stream data?

---

<div class="post-metadata">

**Author:** ![timotheap](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/timotheap/32/26039_2.png) [@timotheap](https://forum.freecodecamp.org/u/timotheap)\
**Post date:** [December 20, 2016, 4:51pm UTC](https://forum.freecodecamp.org/t/twitch-tv-hooking-up-name-and-stream/67520/5 "2016-12-20T16:51:57Z")

</div>

It’s not in the first Ajax request, it’s in the second one.

If I first use the stream request, I don’t have the username etc so I started by the users request.

[edit] I thought I could use the ID to hook up both requests but I don’t know how. I’m obviously missing something…

---

<div class="post-metadata">

**Author:** ![MARKJ78](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/markj78/32/160083_2.png) [@MARKJ78](https://forum.freecodecamp.org/u/MARKJ78)\
**Post date:** [December 20, 2016, 4:56pm UTC](https://forum.freecodecamp.org/t/twitch-tv-hooking-up-name-and-stream/67520/6 "2016-12-20T16:56:50Z")

</div>

Hey @timotheap,

First things first, Instead of having your ajax call in a loop, move it into its own function.

From within your loop you can pass the username `[i]` to the ajax function as an argument.

You only need to make the ajax call once and you can do anything you need for this challenge with the response.

You can of course make more calls if you want to get a bit fancy, but the info you need for the challenge is in the response you’re already getting.

Cheers

Mark

---

<div class="post-metadata">

**Author:** ![timotheap](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/timotheap/32/26039_2.png) [@timotheap](https://forum.freecodecamp.org/u/timotheap)\
**Post date:** [December 20, 2016, 4:57pm UTC](https://forum.freecodecamp.org/t/twitch-tv-hooking-up-name-and-stream/67520/7 "2016-12-20T16:57:43Z")

</div>

I believe I’ve tried that already without success but I must have missed something. I’ll try again, thanks !

---

<div class="post-metadata">

**Author:** ![Oxyrus](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/oxyrus/32/69509_2.png) [@Oxyrus](https://forum.freecodecamp.org/u/Oxyrus)\
**Post date:** [December 20, 2016, 4:58pm UTC](https://forum.freecodecamp.org/t/twitch-tv-hooking-up-name-and-stream/67520/8 "2016-12-20T16:58:27Z")

</div>

`$.getJSON('https://api.twitch.tv/kraken/streams/' + STREAMER_NAME, function(channel) {`

Use name instead of Id

---

<div class="post-metadata">

**Author:** ![timotheap](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/timotheap/32/26039_2.png) [@timotheap](https://forum.freecodecamp.org/u/timotheap)\
**Post date:** [December 20, 2016, 5:00pm UTC](https://forum.freecodecamp.org/t/twitch-tv-hooking-up-name-and-stream/67520/9 "2016-12-20T17:00:03Z")

</div>

I do make an Ajax request for streams (the second Ajax request in my code), but I think it’s in the wrong place and so it doesn’t iterate properly and doesn’t return the appropriate status offline/online for each user.

I’ll try to take my Ajax request out of the loop…

---

<div class="post-metadata">

**Author:** ![MARKJ78](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/markj78/32/160083_2.png) [@MARKJ78](https://forum.freecodecamp.org/u/MARKJ78)\
**Post date:** [December 20, 2016, 5:02pm UTC](https://forum.freecodecamp.org/t/twitch-tv-hooking-up-name-and-stream/67520/10 "2016-12-20T17:02:25Z")

</div>

Hey @timotheap , did you know you can analyse your code in codepen? click the little arrow icon in the corner of the editor.

It works exactly like linting which you might use when you get to using an editor - and they will all tell you off for putting a function in a loop!

---

<div class="post-metadata">

**Author:** ![timotheap](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/timotheap/32/26039_2.png) [@timotheap](https://forum.freecodecamp.org/u/timotheap)\
**Post date:** [December 20, 2016, 5:07pm UTC](https://forum.freecodecamp.org/t/twitch-tv-hooking-up-name-and-stream/67520/11 "2016-12-20T17:07:08Z")

</div>

Oh dear, you’re right… but I have 2 For loops and 2 ajax requests - now I’m totally confused as to where I should put any of it. When I get lost like that I usually start from scratch again - but I’ve already done that three times.

[Edit] Wait, wait, ok, I’m gonna get this maybe not tonight but that’s fine, I just wanted something I could work with and I can work on that.

Thanks a bunch!

---

<div class="post-metadata">

**Author:** ![MARKJ78](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/markj78/32/160083_2.png) [@MARKJ78](https://forum.freecodecamp.org/u/MARKJ78)\
**Post date:** [December 20, 2016, 5:11pm UTC](https://forum.freecodecamp.org/t/twitch-tv-hooking-up-name-and-stream/67520/12 "2016-12-20T17:11:57Z")

</div>

Use your loop to iterate the array and then pass the goods (`[i]`) to a separate function to do the actual deed (ajax call, or DOM manipulation) 🙂

So in effect you want separate loops feeding `[i]`'s to functions.

I can give you some hints if you like, but I know you like to like to figure these things out for yourself

---

<div class="post-metadata">

**Author:** ![Oxyrus](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/oxyrus/32/69509_2.png) [@Oxyrus](https://forum.freecodecamp.org/u/Oxyrus)\
**Post date:** [December 20, 2016, 5:12pm UTC](https://forum.freecodecamp.org/t/twitch-tv-hooking-up-name-and-stream/67520/13 "2016-12-20T17:12:15Z")

</div>

You can always try to use another library to make requests, personally I don’t like the way one does it with Ajax, you could check [axios](https://github.com/mzabriskie/axios).

---

<div class="post-metadata">

**Author:** ![timotheap](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/timotheap/32/26039_2.png) [@timotheap](https://forum.freecodecamp.org/u/timotheap)\
**Post date:** [December 20, 2016, 5:14pm UTC](https://forum.freecodecamp.org/t/twitch-tv-hooking-up-name-and-stream/67520/14 "2016-12-20T17:14:03Z")

</div>

I’ll definitely have a look at that later - but not this time ! Just for the three apps I’ve had to watch Intro to Ajax, Intro to jquery, and some videos on Youtube and I actually like it (ajax)… I’m not strong enough to stray so soon ! But thank you for the info.

---

<div class="post-metadata">

**Author:** ![timotheap](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/timotheap/32/26039_2.png) [@timotheap](https://forum.freecodecamp.org/u/timotheap)\
**Post date:** [December 20, 2016, 5:15pm UTC](https://forum.freecodecamp.org/t/twitch-tv-hooking-up-name-and-stream/67520/15 "2016-12-20T17:15:36Z")

</div>

Ok I think it’s clearer in my head with this last explanation. Yes, I think it is (sorry, I’m thinking and writing at the same time).

Thanks a lot - I really hope I can fix it now.

[edit] it’s just…I find it really puzzling that I can something in a for loop, and that a function outside that will “recognize” my i. Damn it I’ve got to continue reading YDKJS .

---

<div class="post-metadata">

**Author:** ![Oxyrus](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/oxyrus/32/69509_2.png) [@Oxyrus](https://forum.freecodecamp.org/u/Oxyrus)\
**Post date:** [December 20, 2016, 5:15pm UTC](https://forum.freecodecamp.org/t/twitch-tv-hooking-up-name-and-stream/67520/16 "2016-12-20T17:15:49Z")

</div>

Haha, you’re welcome, I’ll try to finish the random quote generator today and start working on this Twitch project 🙂

---

<div class="post-metadata">

**Author:** ![MARKJ78](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/markj78/32/160083_2.png) [@MARKJ78](https://forum.freecodecamp.org/u/MARKJ78)\
**Post date:** [December 20, 2016, 5:37pm UTC](https://forum.freecodecamp.org/t/twitch-tv-hooking-up-name-and-stream/67520/17 "2016-12-20T17:37:59Z")

</div>

> [@timotheap](#):
>
> [edit] it’s just…I find it really puzzling that I can something in a for loop, and that a function outside that will “recognize” my i. Damn it I’ve got to continue reading YDKJS

Thats Ok @timotheap, it will click.

**Don’t look at this** if you want to figure it out yourself! Its just a quick demo of how to pass info between functions.

[details=Summary]You see in this loop, I iterate the array and then use that info to complete API URL. Then I call my fetch function (which is the actual API call). When I call my fetch function I include the url var as an argument.

```
  for (var i = 0; i < userChannels.length; i++) {
        var channel = userChannels[i];
        var url = 'https://api.twitch.tv/kraken/streams/' + channel + '/?client_id=YOURIDHERE';

        //initiate API call
        fetch(url);
    }

```

My fetch function is awaiting the url, because I told it to expect something as an argument.

```
function fetch(url) {
    //api call using url goes here
}

```

When you create a function, if you need some info passed to it for it to work, say so by putting it in the parenthesis. You can add more than one.

For instance -

```
function fetch(url, channel){
   //API call with the url
   // console.log(channel + ' has been called')
   callAnotherFunctionWithThe(response);
}     

for (var i = 0; i < userChannels.length; i++) {
    var channel = userChannels[i];
    var url = 'https://api.twitch.tv/kraken/streams/' + channel + '/?client_id=YOURIDHERE';
     //initiate API call
     fetch(url, userChannels[i] );

```

}

[/details]

Hope that makes a little sense at least!

---

<div class="post-metadata">

**Author:** ![timotheap](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/timotheap/32/26039_2.png) [@timotheap](https://forum.freecodecamp.org/u/timotheap)\
**Post date:** [December 20, 2016, 5:39pm UTC](https://forum.freecodecamp.org/t/twitch-tv-hooking-up-name-and-stream/67520/18 "2016-12-20T17:39:36Z")

</div>

Thanks I’ll keep it for later, either as a reward if I’ve managed or as help if I don’t 🙂

---

<div class="post-metadata">

**Author:** ![timotheap](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/timotheap/32/26039_2.png) [@timotheap](https://forum.freecodecamp.org/u/timotheap)\
**Post date:** [December 20, 2016, 8:13pm UTC](https://forum.freecodecamp.org/t/twitch-tv-hooking-up-name-and-stream/67520/19 "2016-12-20T20:13:20Z")

</div>

Well, I had a look because I wasn’t clear at all with that…and your explanations are great - and I thought I’d be ok.

But I’m still having the same problem as before: when I call to get my stream status (offline/ online), my function doesn’t iterate through the array of users.

Is it because I’m calling the second function after the first one?

[pen](http://codepen.io/timotheap/pen/PbLWmW?editors=1010)

Any chance you could help with that? I thought it was because I was comparing with “null” but that doesn’t change the fact that it only prints for the last user in the array…

---

<div class="post-metadata">

**Author:** ![MARKJ78](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/markj78/32/160083_2.png) [@MARKJ78](https://forum.freecodecamp.org/u/MARKJ78)\
**Post date:** [December 20, 2016, 8:37pm UTC](https://forum.freecodecamp.org/t/twitch-tv-hooking-up-name-and-stream/67520/20 "2016-12-20T20:37:24Z")

</div>

just looking now, you nearly got this.

in fact you did have it but you also have extra’s!

I’ve forked it and cut it down a bit.

[http://codepen.io/mpjones/pen/MbxvBw?editors=0010](http://codepen.io/mpjones/pen/MbxvBw?editors=0010)

I also broke your mustache in my version… but the logs are there!

[Next page](https://forum.freecodecamp.org/t/twitch-tv-hooking-up-name-and-stream/67520.md?page=2)
