0

Having read up on different methods please bear with me to try to explain.

I am trying to retreive data from the twitch api and loop the user.name results to an array possibly inside an object. I am using nodejs so it has to be javascript.

So far when I run the following I get a nice json response.

var request = require('request');

    request({url: 'https://api.twitch.tv/kraken/channels/twitch/follows?limit=3'}, function(err, res, json) {
        if (err) {
            throw err;
        }

        console.log(json);

    });

this then logs the same as if one where to visit https://api.twitch.tv/kraken/channels/twitch/follows?limit=3

or better visualized as

json structure

Now I want to select the follows -> user -> name object. More so, loop every user -> name in the response.

I thought I would need to convert the string to an object so I tried

    var obj = JSON.parse(json);

but that only returns the first {3} objects in the tree. So I went ahead and tried

var request = require('request');

    request({url: 'https://api.twitch.tv/kraken/channels/twitch/follows?limit=3'}, function(err, res, json) {
        if (err) {
            throw err;
        }

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

        var obj = JSON.parse(json.follows[i].user.name);
        console.log(obj);

        }

    });

and it returns

TypeError: Cannot read property '0' of undefined

For testing purposes I also got rid of the loop and just have 1 to return one bit of info. Having tried multiple instances of rearranging the call I always get either an error or "undefined" back.

Nothing seems to work, I am even going about this the right way?

3 Answers 3

2

Loop over json.followers.length - not json.length - json is an object, and objects don't have a length:

for (var i=0; i<json.follows.length; i++) {
Sign up to request clarification or add additional context in comments.

4 Comments

So simple yet so effective.
I seem to get TypeError: Cannot read property 'length' of undefined when trying this.
full edit : for (var i=0; i<json.follows.length; i++) { var obj = JSON.parse(json.user[i].name); console.log(obj); }
Thanks for your help, I marked the below question as the accepted answer. Not sure on the procedure but the extra line helped me even though its pretty much the same thing. Hope this is ok, I upvoted your question.
1

As json is an object here, we should use for-in though there is no length property. But here json.follows is an array. So we should use for loop.

var len = json.follows.length;
for (var i=0; i< len; i++) {
    console.log(json.follows[i].user.name);
}

5 Comments

follows is an array - use a regular for loop for arrays.
yes...for follows we can use a regular for loop. But for json which is an object we should use for..in. I will update my answer.
I get TypeError: Cannot read property 'length' of undefined, the same as the above answer. Not sure how this is happening?
But I tested and its working fine. Can you do a console.log(json) and check what is the output and does it contains "follows" or not?
Yes I needed to include obj = JSON.parse(json); which then solved the issue. many thanks you :)
0

Supposing you do var obj = JSON.parse(json);

for (var j = 0; j < obj.follows.length; j++) {
    var thisName = jsonObj.follows[j].user.name;
    console.log(thisName);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.