desmondmorris / node-twitter

Client library for the Twitter REST and Streaming API's.
https://www.npmjs.com/package/twitter
MIT License
1.27k stars 237 forks source link

Some example of using cursor to navigate thru pages. #258

Closed hrivera2014 closed 6 years ago

hrivera2014 commented 7 years ago

Please I need to use cursor parameter to navigate thru pages. How I can get next_cursor from the list object but outside of the client.get Of course, as you can guess, I m not a nodejs expert and I have to study a lot about asynchronous responses I believe. I'm only a researcher struggling with this stuff..

var params = {screen_name: 'someOne',count: '200'};
client.get('friends/list', params, function(error, list, response)  
{
    if (!error) 
   {
     for (var index in list.users)
       {
     console.log(index);
     console.log("Name: " + list.users[index].name);    
         console.log("Screen name: " + list.users[index].screen_name);
     console.log("followers: " + list.users[index].followers_count);
    }
   }
  });

 cursor = list.next_cursor
hrivera2014 commented 7 years ago

I think this may be the solution, but I have not tried it so far.

https://stackoverflow.com/questions/28008897/node-js-twitter-api-cursors

Also I read about how promises can solve the problem but I don't know nothing about promises until now.

hrivera2014 commented 7 years ago

I find a solution to the problem thru the link above using recursive call. I just rewrite the code as:

var params = {screen_name: 'someOne',count: '200', cursor:  -1};
client.get('friends/list', params, function getlist(error, list, response)  
{
    if (!error) 
   {
     for (var index=0,index<list.users.length-1,index++)
     {
    console.log(index);
    console.log("Name: " + list.users[index].name); 
        console.log("Screen name: " + list.users[index].screen_name);
    console.log("followers: " + list.users[index].followers_count);
      }
     if(list.next_cursor != 0) 
     {
       params.cursor = list.next_cursor
       client.get('friends/list',params, getlist);
     }
  }
)};
hrivera2014 commented 7 years ago

I wonder why the first element of the next page is the same that the last element in before page. By this strange behavior I put the final argument of the for loop as: list.users.length-1.