Using API to retrieve channel status

Hey people,

I’m using react and trying to display different behaviour in order if user’s channel is online or not.

So i get the response but the following code below doesn’t work as it’s always return first statement rather than else even if channel is offline ( in this case that is when array length 0 which is case when user is not online - there is no object )

This is my code

Check out line 35 and 38 - at 35 in the console.log it shows that in the state that length is equal to zero but somehow it always displays this insted of one below in else statement.

Hey guys, anyone? Can’t see like for 15 hours where i’m wrong :confused:

Well you asked a React/Javascript programming issue. Not really a Twitch API issue.

Your Axios code seems wrong.

const axios = require('axios');

axios.defaults.headers.common["Client-ID"] = 'CLIENT';
    axios
      .get("https://api.twitch.tv/helix/streams?user_login=blaschikovsky")
      .then(response => {
        console.log(response.data);

        console.log(response.data.length);

        if (response.data.length !== 0) {
            console.log('Is not zero');
        } else {
            console.log('Is Zero');
        }

      })
      .catch(error => {
        console.log(error);
      });

results in:

{ data: [], pagination: {} }
undefined
Is not zero

The use of !== performs a test and a type cast test. So you’ve tried to see if an OBJECT has length. And of course an object doesn’t

Line 12:

this.setState({data: response.data})

Should be:

this.setState({data: response.data.data})

Response.data fetches the response from twitch, but you probably want the data array in the respone that’ll contain 0/1 streams in an array which is what you can test the length of as it’s an Array.

1 Like

Thank you man, saved my day.

I knew it wasn’t but i was not properly sure if i’m calling it and by that i mean with response.data as when i looked into console to debug it looked right.

Thank you again!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.