Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

What does this documentation mean?

I'm working on a project with the Twitch API, and I've come across something a bit confusing: https://github.com/justintv/Twitch-API/blob/master/v3_resources/streams.md#get-streamschannel

In the docs, if you find the 'if offline' section and 'if online' section, something weird occurs. If the channel I'm passing in to the api is offline, I can't find a way to display it's name. Then if it's offline, I can only display the url, not the name of the channel or stream, which is kind of confusing. Am I just missing something obvious here? Thanks, G

1 Answer

Joel Bardsley
Joel Bardsley
31,249 Points

Assuming you're already passing the channel name into the Twitch API as part of the url for your GET request, you could always assign the channel name to a separate variable that you can refer to later on for displaying or anything else, and concatenate it to the API url like this:

var channelName = "Quill18"; // The best!
var url = "https://api.twitch.tv/kraken/streams/" + channelName;

// Run your $.ajax / $.getJSON script passing in the url as a property/parameter ie:

$.getJSON(url, function(data){

  // If channel is offline, the returned 'stream' property is null (as shown in the API docs)
  // You can use this to apply your own functionality based on the channel being currently offline or online:

  if(data.stream === null) {
    $('#someElement').html(channelName + " is currently offline.");
  } else {
    $('#someElement').html(channelName + " is online!");
    // Use data.stream properties you now have available to extend this further eg show viewer/follower count 
  }

});

You might not be using jQuery or might be using a different version of the Twitch API, but hopefully this helps to give you some idea of how you can approach your project. Good luck!

Totally! I'm really learning to think 'outside the api'. Unless of course I instantiate a search-and-add feature, I don't need to actually pull the channel name from the object, I can just manually add it.