These are chat archives for MontCode/GeneralChat

26th
Aug 2016
Lily Romano
@Lilyheart
Aug 26 2016 00:47
@dcpking @kevincauto @angelinasimms and anyone else has has met at the library both upstairs and down? Should I just stick with upstairs with what seems better Wifi and definitely better power but less setting? Or try to get downstairs when I can?
At least for while I'm trying to find a better space to meet up?
Lily Romano
@Lilyheart
Aug 26 2016 11:59
http://i.imgur.com/fuDDhdL.jpg
Victor
@VictorHall
Aug 26 2016 14:59
dies any know how to test if an object is null in js? I keep getting a typeError
does*
Jonathan Herman
@lantech19446
Aug 26 2016 15:00
just console.log the value of the variable you're checking
Victor
@VictorHall
Aug 26 2016 15:03

function createList(statuses, allusers, length){

var list = document.createElement("ul");

for (var i = 0; i < length; i++){

var anchor = document.createElement("a");
anchor.href = "https://twitch.tv/" + allusers[i];

if( typeof statuses[i].stream == undefined  ){
   anchor.innerText = allusers[i] + "is offline";
}else{

  anchor.innerText = allusers[i] + " is online " + statuses.stream.channel.game;
}

var elem = document.createElement("li");
elem.appendChild(anchor);
list.appendChild(elem);

}

var div = document.getElementById("userList");
div.appendChild(list);

}

Uncaught TypeError: Cannot read property 'channel' of undefined
so if a user is online then the stream object will have the information about what games that they are playing
if the object is null then they are offline
im having a hard time accuratly testing for null
Jonathan Herman
@lantech19446
Aug 26 2016 15:06
you mean to see if it's actually setting itself null when a user is offline?
Lily Romano
@Lilyheart
Aug 26 2016 15:21
What is populating the statuses parameter? Is the key value pair statuses[i].stream non existent when offline? Or is the key there but the value is null? Does the following code work?
if (statuses[i].stream) {
David Castner
@davidjcastner
Aug 26 2016 16:28

@VictorHall when using typeof make sure to check against a string

const someVariable = undefined;
console.log(typeof someVariable == undefined); //false
console.log(typeof someVariable === 'undefined'); //true

To test is something is null:

const someVariable = null;
console.log(someVariable === null); //true
console.log(undefined === null); //false
also to access an object's property but if you are unsure if the object exists:
const someProperty = 'something';
const someObject = undefined;
if (someObject[someProperty]) { } // Type Error: Cannot read property 'something' of undefined
if (someObject && someObject[someProperty]) { }// false (no errors)
David Castner
@davidjcastner
Aug 26 2016 16:37
@VictorHall hope that helps
Victor
@VictorHall
Aug 26 2016 17:14
@Lilyheart its if statuses[i].stream is null.
@Lilyheart still Uncaught TypeError: Cannot read property 'channel' of undefined
Are you using the twitch API? https://github.com/justintv/twitch-api#index
David Castner
@davidjcastner
Aug 26 2016 17:51
@VictorHall you are trying to access the channel property of an object that isn't defined
It's not in the section of code you sent us
.
Victor
@VictorHall
Aug 26 2016 17:52

$( "#all" ).click(function() {
//alert( "Handler for .click() called." );
$("div#userList").empty();
//clears the list so that new data can be inserted
//theses are the usernames that we will be using
var users = ["PGX_lPoLoMaNl", "SwissSoldat", "CrReaM", "Geoff", "storbeck", "minimacher", "SpinBros", "medrybw"];

var statuses = [];//status of users

for (var i = 0; i < users.length; i++){
getStatus(users[i], users.length, statuses, i, users);
}

});

function getStatus(user, length, statuses, index, allusers){
//makes api call for each user

$.ajax({
url:'https://api.twitch.tv/kraken/streams/' + user,
dataType:'jsonp',
success:function(channel) {
//logic here
//creates a new list only once

  statuses.push(channel);

    if(index == (length -1)){
createList(statuses, allusers, length)
console.log(index + " length: " + length + " execute")

}
}//end of success
});//end of ajax

}

function createList(statuses, allusers, length){

var list = document.createElement("ul");

for (var i = 0; i < length; i++){

var anchor = document.createElement("a");
anchor.href = "https://twitch.tv/" + allusers[i];
const test = statuses[i].steam;
if(statuses[i].stream){
anchor.innerText = allusers[i] + "is offline";
}else{

  anchor.innerText = allusers[i] + " is online " + statuses.stream.channel.game;
}

var elem = document.createElement("li");
elem.appendChild(anchor);
list.appendChild(elem);

}

var div = document.getElementById("userList");
div.appendChild(list);

}

Lily Romano
@Lilyheart
Aug 26 2016 17:55
Can you edit your post and put three ``` in the line before and the line after the code? Thats the backtick aka grave symbol found on the tilde ~ key above tab usually.
Victor
@VictorHall
Aug 26 2016 17:56
                  $( "#all" ).click(function() {
              //alert( "Handler for .click() called." );
               $("div#userList").empty();
              //clears the list so that new data can be inserted
             //theses are the usernames that we will be using
              var users = ["PGX_lPoLoMaNl", "SwissSoldat", "CrReaM", "Geoff", "storbeck", "minimacher", "SpinBros", "medrybw"];

             var statuses = [];//status of users

            for (var i = 0; i < users.length; i++){
           getStatus(users[i], users.length, statuses, i, users);         
           }

        });


        function getStatus(user, length, statuses, index, allusers){
      //makes api call for each user

      $.ajax({ 
     url:'https://api.twitch.tv/kraken/streams/' + user,
     dataType:'jsonp',
     success:function(channel) { 
      //logic here
      //creates a new list only once

       statuses.push(channel);

        if(index == (length -1)){
        createList(statuses, allusers, length)
         console.log(index + " length: " + length + " execute")
         }
        }//end of success
        });//end of ajax   

       }

    function createList(statuses, allusers, length){

    var list = document.createElement("ul");

    for (var i = 0; i < length; i++){

    var anchor = document.createElement("a");
    anchor.href = "https://twitch.tv/" + allusers[i]; 

    if(statuses[i].stream){
   anchor.innerText = allusers[i] + "is offline";
    }else{

    anchor.innerText = allusers[i] + " is online " + statuses.stream.channel.game;
    }

   var elem = document.createElement("li");
   elem.appendChild(anchor);
   list.appendChild(elem);
  }

   var div = document.getElementById("userList");
  div.appendChild(list);
   }
this is pretty funky but....yea
so i pass the reference of statuses to getStatus then if pushes the object return of the specific user back to statuses
if the end of the array is reached it passes that reference of statuses to createList
i loop through the array of statuses to create my list and display the result
i need to display the status of the user
if the user is offline then statuses.stream is null
Lily Romano
@Lilyheart
Aug 26 2016 18:00
Here is an example of an object that is returned from the API https://github.com/justintv/Twitch-API/blob/master/v3_resources/streams.md#example-response-1 The problem is digging through the JSON to get what you want. Gimme a minute to look at the syntax.
Victor
@VictorHall
Aug 26 2016 18:00
if the users is online then statuses .stream is not null and it contains information about the game that they are playing
when i loop through to test if they statuses.stream == null it gives me a type error every time
Lily Romano
@Lilyheart
Aug 26 2016 18:01
gimme a minute...
Victor
@VictorHall
Aug 26 2016 18:01
ive tried every possible google search
okay thanks
@Lilyheart @davidjcastner
Lily Romano
@Lilyheart
Aug 26 2016 18:04
Your error states the problem is encountered when you get to the point of trying to find channel.
If you look at that sample array data I linked above, you will see that the value of the streams key is actually an array.
If I take that same data and save it to a variable called statuses, I can print the game by using the following code console.log(statuses.streams[0].channel.game)
the biggest issue when parsing out data is learning when you have to use object key/value pairs to access and when you need to use array index to access.
{} = object
[] = array
as you dig deeper into the data to get what you want, always look and see if it's an object or an array. That will let you know the syntax needed for that part of the chain.
Victor
@VictorHall
Aug 26 2016 18:07
one sec, im about to make changes to see if it works
Lily Romano
@Lilyheart
Aug 26 2016 18:08
I don't know the API well enough to know if you will need to for loop through a streams array or if there is only ever one.
Victor
@VictorHall
Aug 26 2016 18:23
        var test = statuses[i].stream[0];

          if( test === null ){
         anchor.innerText = allusers[i] + "is offline";
      }else{

         anchor.innerText = allusers[i] + " is online " + statuses.stream[0].game;
       }
Uncaught TypeError: Cannot read property '0' of null
@Lilyheart
Lily Romano
@Lilyheart
Aug 26 2016 18:24
you are missing channel.
Victor
@VictorHall
Aug 26 2016 18:27
streams also has a game object as well as channel
Lily Romano
@Lilyheart
Aug 26 2016 18:28
streams? or stream? You have stream singular.
Victor
@VictorHall
Aug 26 2016 18:28
stream
Lily Romano
@Lilyheart
Aug 26 2016 18:28
Not what the sample data says. You might want to check.
Victor
@VictorHall
Aug 26 2016 18:29
lol
Lily Romano
@Lilyheart
Aug 26 2016 18:30
Based solely on the example response data from https://github.com/justintv/Twitch-API/blob/master/v3_resources/streams.md#example-response-1
=
You have to walk through the data.
  1. Start at the top. The object is called statuses. So you start with statuses.
  2. Next you want inside the streams key. So now your code is statuses.streams
  3. Streams is an array [] **I don't know your object data, there maybe be more then one item in the array, the sample only has one. Assuming you want the first one, your code is now statuses.streams[0]
  4. Next you want the channel key, So now your code is statuses.streams[0].channel
  5. Lastly you want the game key, so you end up with statuses.streams[0].channel.game
Victor
@VictorHall
Aug 26 2016 18:30
still getting the same error
Lily Romano
@Lilyheart
Aug 26 2016 18:33
In the console, where you are seeing your error, can you type console.log(statuses) and let us know what is in that? I would expect something like this
blob
Victor
@VictorHall
Aug 26 2016 18:33
statuses is an array of objects
each array element contain an object that corresponds to the name
Lily Romano
@Lilyheart
Aug 26 2016 18:34
I know. I need to SEE the object.
Victor
@VictorHall
Aug 26 2016 18:34
var users = ["PGX_lPoLoMaNl", "SwissSoldat", "CrReaM", "Geoff", "storbeck", "minimacher", "SpinBros", "medrybw"];
Lily Romano
@Lilyheart
Aug 26 2016 18:34
I can no longer guess what data you have. I'm going off of the sample data from the API.
Unless you can send me that screenshot, then I don't even know if we are talking about the same thing.
Victor
@VictorHall
Aug 26 2016 18:34
names go through ajax request
Lily Romano
@Lilyheart
Aug 26 2016 18:34
I can no longer guess what data you have. I'm going off of the sample data from the API.
Unless you can send me that screenshot, then I don't even know if we are talking about the same thing.
Victor
@VictorHall
Aug 26 2016 18:35
and are pushed into statuses
screenshot of my code?
Lily Romano
@Lilyheart
Aug 26 2016 18:36
In the console, where you are seeing your error, can you type console.log(statuses) and let us know what is in that? I would expect something like this
blob
or even easier, just in your code put in console.log(statues) at the start of the function giving you issues.
actually I can almost guess what the issue is, but I'd like to see that log output first.
Victor
@VictorHall
Aug 26 2016 18:38
                  $( "#all" ).click(function() {
              //alert( "Handler for .click() called." );
               $("div#userList").empty();
              //clears the list so that new data can be inserted
             //theses are the usernames that we will be using
              var users = ["PGX_lPoLoMaNl", "SwissSoldat", "CrReaM", "Geoff", "storbeck", "minimacher", "SpinBros", "medrybw"];

             var statuses = [];//status of users

            for (var i = 0; i < users.length; i++){
           getStatus(users[i], users.length, statuses, i, users);         
           }

        });


        function getStatus(user, length, statuses, index, allusers){
      //makes api call for each user

      $.ajax({ 
     url:'https://api.twitch.tv/kraken/streams/' + user,
     dataType:'jsonp',
     success:function(channel) { 
      //logic here
      //creates a new list only once

       statuses.push(channel);

        if(index == (length -1)){
        createList(statuses, allusers, length)
         console.log(index + " length: " + length + " execute")
         }
        }//end of success
        });//end of ajax   

       }

    function createList(statuses, allusers, length){

    var list = document.createElement("ul");

    for (var i = 0; i < length; i++){

    var anchor = document.createElement("a");
    anchor.href = "https://twitch.tv/" + allusers[i]; 

    if(statuses[i].stream){
   anchor.innerText = allusers[i] + "is offline";
    }else{

    anchor.innerText = allusers[i] + " is online " + statuses.stream.channel.game;
    }

   var elem = document.createElement("li");
   elem.appendChild(anchor);
   list.appendChild(elem);
  }

   var div = document.getElementById("userList");
  div.appendChild(list);
   }
the line that i am having problems with starts in the createList function
Lily Romano
@Lilyheart
Aug 26 2016 18:39
I know that.
I need to see what is actually in statuses.
I don't have your full code to run so I can't so it.
Victor
@VictorHall
Aug 26 2016 18:40
blob
would you be able to use my pen to run?
console.log(statuses);
blob
Lily Romano
@Lilyheart
Aug 26 2016 18:42

Okay, so lets go back to my "walk through the data" steps. What do you see is wrong with my assumption? That might fix your problem.

You have to walk through the data.

  1. Start at the top. The object is called statuses. So you start with statuses.
  2. Next you want inside the streams key. So now your code is statuses.streams
  3. Streams is an array [] **I don't know your object data, there maybe be more then one item in the array, the sample only has one. Assuming you want the first one, your code is now statuses.streams[0]
  4. Next you want the channel key, So now your code is statuses.streams[0].channel
  5. Lastly you want the game key, so you end up with statuses.streams[0].channel.game
Victor
@VictorHall
Aug 26 2016 18:42
blob
Lily Romano
@Lilyheart
Aug 26 2016 18:43
What is your top? I assumed it was an object. So you need to replace step 1 with something else.
hint
blob
Victor
@VictorHall
Aug 26 2016 18:43
statuses is an array of objects
Lily Romano
@Lilyheart
Aug 26 2016 18:43
Woot! So what's missing?
take that console.log and make sure each of the arrows you click to dig in matches each step in your command (e.g. array[0].key.key.array[0]). If you skip something in your command, then it won't pull the right information.
Lily Romano
@Lilyheart
Aug 26 2016 18:54
Looking at the pen, here is the code from just one of the objects in statuses. You'll need to figure out the command path to get to what you want.
blob
Victor
@VictorHall
Aug 26 2016 18:56
stream seems to be an object here which is different from the previous documentation
Lily Romano
@Lilyheart
Aug 26 2016 18:56
yup! Welcome to the world of crappy documentation! \^^/
During that CIS111B project, Lee and I spent HOURS doing this object/array/json data digging before we got it all figured out. HOURS! Then we danced in my living room, literally, when we finally got it working.
Victor
@VictorHall
Aug 26 2016 18:57
i can not wait to get to the point of dancing
Lily Romano
@Lilyheart
Aug 26 2016 18:59

When I come across crappy documented code I will literally go piece by piece.
console.log(statuses); Thought: "okay, that's an array, I want the first one."
console.log(statuses[0]); Thought: "okay, that's an object, I want the stream key."
console.log(statuses[0].stream); Thought: "okay, that's an object, I want the game key."
console.log(statuses[0].stream.game); Bingo!

THEN I figure out my for loops as needed and replace any indexes console.log(statuses[i].stream.game);

Victor
@VictorHall
Aug 26 2016 19:00
im so dancing right now!!
Lily Romano
@Lilyheart
Aug 26 2016 19:00
YAY!
Victor
@VictorHall
Aug 26 2016 19:01
blob
Lily Romano
@Lilyheart
Aug 26 2016 19:01
PARTY TIME!
Victor
@VictorHall
Aug 26 2016 19:01
it weird though because i had to press the button "all" twice
and it still gave me a weird error
Lily Romano
@Lilyheart
Aug 26 2016 19:01
Digging into JSON data really can suck the first few times. Until you hit all the pitfalls. Soon it'll be second nature to you?
Victor
@VictorHall
Aug 26 2016 19:02
Uncaught TypeError: Cannot read property 'stream' of undefined
my first issue was test whether the object was null first
then it was stepping through it
Lily Romano
@Lilyheart
Aug 26 2016 19:03
Add in more console logs that give you more information. Not this exact code, because I don't know where you issue is, but something like console.log("On for loop " + i + " the thing I'm looking at is\n " + statuses[i])
If you put something like that as the very first line in your for loop, the error should be right after the comment. So then just find the error and check the data above it.
afk a bit. Too long of a break from workin' on stuff.
Victor
@VictorHall
Aug 26 2016 19:13
i was watching a ted talk
okay. ill give this a shot
the error is appearing in my getStatus function
right after i call the createList function
Victor
@VictorHall
Aug 26 2016 19:18
i occurs every 3 times i repress the button "All"
@Lilyheart
do you think i should use a try catch ?
Victor
@VictorHall
Aug 26 2016 19:24
try{}catch(err){} worked with no error but now the data is inaccurate, players that are online are now set to offline.
ill play around with it
Lily Romano
@Lilyheart
Aug 26 2016 21:31
@davidjcastner I think we were sharing funny phonenumber inputs at one point. Have you seen these? http://imgur.com/gallery/r102w
David Castner
@davidjcastner
Aug 26 2016 21:35
Those were great!!! I love the ones that take extra effort to make lime chatting the digits or drawing them