How to account for api data being "null" or undefined

How to account for api data being "null" or undefined
0

#1

I am on the twitch api challenge and almost done. I have not been able to figure out how to account for FCC’s stream being 'null’
I set the variable ‘check’ = data.stream which is the point where the info becomes null but it didn’t seem to work. My code is below to further illustrate

currentStatus=data.stream.channel.status;
check=data.stream;

if(check==='null'|| check==='undefined'||check===-1){
  currentStatus = 'Offline';
}

$('#info'+ i).append('<p id="info"'+i+'>'+currentStatus+'</p>');
$('#link'+ i).attr('href','https://www.twitch.tv/'+userNames);

});

});

I thought that if check came up as the string null undefined or -1 it would pass the value of ‘Offline’ to the currentStatus variable (the variable that accounts for the name of the stream), but it doesn’t seem to be doing that. Instead it’s just blank.

Here is my codepen for further clarification. As you can see only FCC doesn’t have a stream but instead of saying Offline it’s just empty space.

https://codepen.io/DebugAyem/pen/qXVvbW


#2

I am going to give you a hint here.

First, check the console, because you have the following error that you will need to deal with:

Uncaught TypeError: Cannot read property 'channel' of null
    at Object.success (pen.js:164:30)
    at i (https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js:2:28017)
    at Object.fireWith [as resolveWith] (https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js:2:28783)
    at A (https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js:4:14035)
    at XMLHttpRequest.<anonymous> (https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js:4:16323)

Second, the following if statement is not correct:

  if(check==='null'|| check==='undefined'||check===-1 || check===false){
      currentStatus = 'Offline';
  }

There is a big difference between ‘null’ and null. One is a string and the other is truly the value of null. The same goes for undefined.

Question for you: Your if statement also asks if check === -1 || check === false. When would check every be those values?


#3

Yeah I realized that check wouldn’t ever be -1 but I am still not sure if null/undefined/false are interchangeable (it seems to me now that they are not).

I did see that console error, would I approach that by using a typeOf check? (I’m still very fuzzy on type and typeOf checks and some of that console output looks like gibberish to me). I did try to remedy that by setting up the check variable(obviously unsuccesful).

I did first compare check to null and undefined (no quotes but that didn’t work so I included the quotes) therefore it seems that’s not the main issue here.


#4

The reason it did not work without the quotes, is because you the error came before the if statement. When JS encounters and error, it does not proceed any further. As a test, comment out the line giving the error and see what shows for Free Code Camp. Don’t forget to get rid of quotes around null and undefined.


#5

Friendly reminder that null and undefined are both falsy.


#6

I don’t see how to work around that error because the line causing the error (I assume) is also the line that allows the channels that do have stream data to be printed to html. I looked at the console and the property comes up as null so I assumed the check variable would have accounted for that and there would be no error. I see where the error is I just don’t know how to fix it. Now I’m seeing setting currentStatus to ‘Offline’ may actually be doing more bad than good??


#7

Let’s breakdown the logic:

You only want to show “Offline” when data.stream is null, otherwise show the stream information.

That is a perfect if else statement scenario. You can do this!


#8

Dammit I hoped you wouldn’t say that. If that’s the case then would I have to rewrite my array??

edit: Nope just tried it. Ok cool thanks for the help!! Those tricky if else statements haha