Saturday, 1 April 2017

node.js, for-loops, and twitter API

I am trying to only fetch the text of the tweets for a twitter bot I am working on (keep in mind twitter's API docs are worse than Oracles, which I never thought that was possible, anyway). I was using the following link to make my bot https://community.risingstack.com/node-js-twitter-bot-tutorial/

I assume that it is within their randomTweet function where they get the index from.

function ranDom (arr) {  
  var index = Math.floor(Math.random()*arr.length);
  return arr[index];
};

and later in their code they would reference it using

Twitter.post('favorites/create', {id: randomTweet.id_str}

I am trying to simply access the text using a regular foreach loop (seeing as, as far as I am aware, node.js has no concept of an indexed for loop) as follows:

var tweet = data.statuses;



    for(var result in tweet) {
        console.log(result.id_str);
    }  

However, all I am getting is undefined, and I am not certain as to why since for them it worked fine when they passed in a specific index.



via SomeStudent

No comments:

Post a Comment