0

I am writing a Twitter stream app using nodejs and npm twitter. I have a stream function which works fine but the get function is returning undefined variables.However, if I log the returned JSON for the get function, all the variables are there.

This is the function that works and a snippet of the returned object:

twit.stream('statuses/filter' , {track: 'javascript'}, function(stream) {
    stream.on('data' , function(data) {
            process_data(data);
            console.log(data);
    });
});

{ created_at: 'Fri Aug 25 01:25:59 +0000 2017',
  id: 900891977406849000,
  id_str: '900891977406849024',
  text: 'RT @codemouse: Getting the crowd going with @es_della @BostonNode #nodejs #javascript #meetup,
  source: '<a href="http://twitter.com/download/android" rel="nofollow">Twitter for Android</a>',
  truncated: false,
  in_reply_to_status_id: null,
  in_reply_to_status_id_str: null,
  in_reply_to_user_id: null,
  in_reply_to_user_id_str: null,
  in_reply_to_screen_name: null,
  user: 
   { id: 606678794,
     id_str: '606678794',
     name: 'Designer Tweets',
     screen_name: 'arafkarim',
     location: null,
     url: 'https://www.linkedin.com/in/arafkarim/',

This is the function that dosen't work and a snippet of the returned object:

var params = {screen_name: 'bournemouthuni'};
twit.get('statuses/user_timeline', params, function(error, data, response) {
    process_data(data);
    console.log(data);  
}); 

[ { created_at: 'Thu Aug 24 14:53:33 +0000 2017',
    id: 900732820045385700,
    id_str: '900732820045385728',
    text: '“BU prepared me for my further studies and career" - Katie Ryan. This is her #BUProud story &gt;… ',
    truncated: true,
    entities: 
     { hashtags: [Object],
       symbols: [],
       user_mentions: [],
       urls: [Object] },
    source: '<a href="http://www.hootsuite.com" rel="nofollow">Hootsuite</a>',
    in_reply_to_status_id: null,
    in_reply_to_status_id_str: null,
    in_reply_to_user_id: null,
    in_reply_to_user_id_str: null,
    in_reply_to_screen_name: null,
    user: 
     { id: 56745793,
       id_str: '56745793',
       name: 'Bournemouth Uni',
       screen_name: 'bournemouthuni',
       location: 'Bournemouth, UK',

and here is the process_data function

var process_data = function(data) {
    var object  = {};
    object.date = data.created_at;
    object.text = data.text;
    object.name = data.user.name;
    object.screen_name = data.user.screen_name;
    object.imgURL = data.user.profile_image_url;
    object.bg_image = data.user.profile_background_image_url;
    connection.sendUTF(JSON.stringify(object));

The only difference I can see is the leading [ on the second object but not sure how to get past it :/

  • `process_data(data[0]);` or `for (var one_data in data) process_data(one_data);`. – Ken Y-N Aug 25 '17 at 01:52
  • Possible duplicate of [Loop through an array in JavaScript](https://stackoverflow.com/questions/3010840/loop-through-an-array-in-javascript) – Ken Y-N Aug 25 '17 at 01:52
  • Ahh I see now, the first returns an object and the second an array of objects. I can't believe i didn't spot that! Thank you :) – user1756180 Aug 25 '17 at 04:05

0 Answers0