Twitch TV API - FreeCodeCamps own example doesn't work - streams (null, undefined)

I’m struggling to find a one-pass method to distinguish between:

  1. Active account and streaming
  2. Active account but not currently streaming
  3. Not active account

The three calls I have tried are the endpoint + /streams/, endpoint + /users/ and the endpoint + /channels/

In the end I peeked at the example FreeCodeCamp give on CodePen but noticed their code doesn’t work either.

Specifically this:
if (data.stream === null) {
game = “Offline - undefined e.g. comster404 shouldn’t appear”;
status = “offline”;
} else if (data.stream === undefined) {
game = “Account Closed”;
status = “offline”;
} else {
game = data.stream.game;
status = “online”;
};

Problem is the ‘stream’ parameter does not return ‘undefined’ when the “Account Closed” - it returns ‘null’.
e.g.
https://wind-bow.glitch.me/twitch-api/streams/brunofin

returns
{“stream”:null,"_links":{“self”:“https://api.twitch.tv/kraken/streams/brunofin",“channel”:"https://api.twitch.tv/kraken/channels/brunofin”}}

Perhaps this is due to the change to the endpoint from …kraken… to …wind-blow…?

Just as:
https://wind-bow.glitch.me/twitch-api/streams/freecodecamp

returns
{“stream”:null,"_links":{“self”:“https://api.twitch.tv/kraken/streams/freecodecamp",“channel”:"https://api.twitch.tv/kraken/channels/freecodecamp”}}

So in summary:

  • if you use /streams/ --> Active and broadcasting returns a stream, not broadcasting and non-active return null
  • if you use /channels/ --> Active (Broadcasting and non-broadcasting) both return some info (non of which indicates they are actively streaming) and non-active returns error == Not Found.
  • Similarly, if you use /users/ --> Active (Broadcasting and non-broadcasting) both return some info (non of which indicates they are actively streaming) and non-active returns error == Unprocessable Entity

Clearly you need two calls but currently struggling (even using ‘promises’) to reconcile the two pieces of information asynchronously.

I agree that the code in the example is currently not working, and I suspect you are right that it’s due to a change in the endpoint. Having said that, the code was most likely valid before this change because querying a non-existent used to return this:

 {
    "error": "Not Found",
     "status": 404,
     "message": "Channel 'not-a-valid-account' does not exist"
 }

And data.stream would return undefined. It is worth noting that I think no promises are currently used in the code example.

I had a quick look at the new curriculum in beta and it doesn’t seem to be there. Perhaps that’s the best solution given the trouble this project seems to have caused. d:

Thanks, I’ll keep playing around. To be the honest the ‘promises’ was 50% trying to solve problem and 50% just learning something new. What I love about FCC is that it really encourages you to experiment.