我正在使用nodejs和npm twitter编写Twitter流应用程序。我有一个流函数,它工作正常,但get函数返回未定义的变量。但是,如果我为get函数记录返回的JSON,那么所有变量都在那里。
这是有效的函数和返回对象的片段:
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/',
这是一个不起作用的函数和返回对象的片段:
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 >… ',
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',
这是process_data函数
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));
我能看到的唯一区别是领先[在第二个对象上但不确定如何超越它:/