0

So I am making a request to the new twitch API and I'm having trouble getting the data from the response.

{  
   data:[  
      {  
         id:'28793268096',
         user_id:'71092938',
         game_id:'488552',
         community_ids:[  

         ],
         type:'live',
         title:'NEW EVENT POG ~ twitter.com/xqc',
         viewer_count:9143,
         started_at:'2018-05-22T17:09:12Z',
         language:'en',
         thumbnail_url:'https://static-cdn.jtvnw.net/previews-ttv/live_user_xqcow-{width}x{height}.jpg'
      },
      {  
         id:'28792267216',
         user_id:'23220337',
         game_id:'488552',
         community_ids:[  
            Array
         ],
         type:'live',
         title:'Overwatch Ranked Season 2 Episode 16: Panic! On The Payload | !charity | !patch',
         viewer_count:2332,
         started_at:'2018-05-22T15:25:47Z',
         language:'en',
         thumbnail_url:'https://static-cdn.jtvnw.net/previews-ttv/live_user_emongg-{width}x{height}.jpg'
      }
   ],
   pagination:{  
      cursor:'eyJiIjpudWxsLCJhIjp7Ik9mZnNldCI6Mn19'
   }
}

Using the code below I'm not getting any data from the array.

var reply = axios.get('https://api.twitch.tv/helix/streams?game_id=488552&type=live&first=2&language=en')
    .then(function(response){
        var datas = response.data;
        //console.log(datas);
        for(var i = 0; i < datas.length; i++) {
            var cube = datas[i];
            for(var j = 0; j < cube.length; j++) {
                msg.reply(cube.id);
            }
        }
}).catch((error) => { console.log(error); });;

Any help would be greatly appreciated.

Barmar
  • 741,623
  • 53
  • 500
  • 612
stephendev
  • 91
  • 1
  • 1
  • 9
  • i solved the problem. I just appended another .data on the response and it worked fine – stephendev May 22 '18 at 20:57
  • Isn't what you're doing causing you to fire `msg.reply(cube.id);` once for every property of the 'cube' object? Is that what you want? – dmgig May 23 '18 at 17:06

4 Answers4

1

The values contained within the data array are objects, and thus, do not have a length property. So when you are working on cube.length, the expression is false and not running msg.reply(cube.id). Once you set cube you should be working on it directly.

Without knowing what you're attempting to do, I cannot provide much more guidance.

Jeremy Dentel
  • 837
  • 2
  • 9
  • 19
0

if you're using a modern browser with javascript ES6 this should work. The "cube" objects are just children of the data array, so you just forEach over the array and grab the ids.

response.data.forEach((el, i) => {
  msg.reply(el.id);
});

Here's a fiddle: https://jsfiddle.net/tr7zmrw7/1/

if you want a less modern way, you can do:

for(var i in response.data){
  msg.reply(response.data[i].id);
}
dmgig
  • 4,400
  • 5
  • 36
  • 47
0

Looks like it's because you aren't sending an authorization token

Here's your example simplified. You can see it's hitting the error block and showing:

{
  "error": "Unauthorized",
  "status": 401,
  "message": "Must provide a valid Client-ID or OAuth token"
}

var reply = axios.get('https://api.twitch.tv/helix/streams?game_id=488552&type=live&first=2&language=en')
    .then(function(response){

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

  console.log('got error')
  console.log(error.response.data);

});
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
Community
  • 1
  • 1
Kevin Jantzer
  • 9,215
  • 2
  • 28
  • 52
0

You don't loop through an object like that.

You can msg.reply the whole object:

var reply = axios.get('https://api.twitch.tv/helix/streams?game_id=488552&type=live&first=2&language=en')
    .then(function(response){
        var datas = response.data;
        //console.log(datas);
        for(var i = 0; i < datas.length; i++) {
            msg.reply(datas[i]);
        }
}).catch((error) => { console.log(error); });

Or loop through the object like:

var reply = axios.get('https://api.twitch.tv/helix/streams?game_id=488552&type=live&first=2&language=en')
        .then(function(response){
            var datas = response.data;
        for(var i = 0; i < datas.length; i++) {
            for (var key in datas[i]) {
                if (!datas[i].hasOwnProperty(key)) continue;
                var obj = datas[key];
                for (var prop in obj) {
                    if(!obj.hasOwnProperty(prop)) continue;
                    console.log(prop + " = " + obj[prop]);
                }
            }
        }
    }).catch((error) => { console.log(error); });
NSTuttle
  • 3,237
  • 2
  • 17
  • 26