0

I have done a request to get the tweets from a specified account using https://www.npmjs.com/package/twitter however, I cannot access the "text" part of the JSON object that is returned.

This is an example part of the JSON that is returned from the request:

[ { created_at: 'Sat Apr 08 10:16:51 +0000 2017',
    id: 850653659025334300,
    id_str: '850653659025334272',
    **text: 'Who gets your pick on #GrandNational2017 day? \n\nOur Pinstickers\' guide to the 40-horse race\n\nā€¦ ',
    truncated: true,
    entities:
     { hashtags: [Object],
       symbols: [],
       user_mentions: [],
       urls: [Object] },
    source: '<a href="http://www.socialflow.com" rel="nofollow">SocialFlow</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: 265902729,
       id_str: '265902729',
       name: 'BBC Sport',
       screen_name: 'BBCSport',
       location: 'MediaCityUK, Salford',
       description: 'Official account. Also from @bbc - @bbcmotd @bbcf1 @bbctms @bbctennis @bbcrugbyunion & @bbcgetinspired',
       url: '',
       entities: [Object],
       protected: false,
       followers_count: 6620652,
       friends_count: 264,
       listed_count: 17733,
       created_at: 'Mon Mar 14 09:44:40 +0000 2011',
       favourites_count: 161,
       utc_offset: -36000,
       time_zone: 'Hawaii',
       geo_enabled: true,
       verified: true,
       statuses_count: 263835,
       lang: 'en',
       contributors_enabled: false,
       is_translator: false,
       is_translation_enabled: true,
       profile_background_color: 'C0DEED',
       profile_background_image_url: 'http://pbs.twimg.com/profile_background_images/240463705/BBCSportTwitter_backing11.jpg',
       profile_background_image_url_https: 'https://pbs.twimg.com/profile_background_images/240463705/BBCSportTwitter_backing11.jpg',
       profile_background_tile: false,
       profile_image_url: 'http://pbs.twimg.com/profile_images/803606866974609412/Ymsnopmj_normal.jpg',
       profile_image_url_https: 'https://pbs.twimg.com/profile_images/803606866974609412/Ymsnopmj_normal.jpg',
       profile_banner_url: 'https://pbs.twimg.com/profile_banners/265902729/1490963959',
       profile_link_color: '0084B4',
       profile_sidebar_border_color: 'C0DEED',
       profile_sidebar_fill_color: 'DDEEF6',
       profile_text_color: '333333',
       profile_use_background_image: true,
       has_extended_profile: false,
       default_profile: false,
       default_profile_image: false,
       following: true,
       follow_request_sent: false,
       notifications: false,
       translator_type: 'none' },
    geo: null,
    coordinates: null,
    place: null,
    contributors: null,
    is_quote_status: false,
    retweet_count: 0,
    favorite_count: 6,
    favorited: false,
    retweeted: false,
    possibly_sensitive: false,
    lang: 'en' } ]

I am simply wanting to console.log the text: part of the JSON, which I have marked with **. Here is my code:

const Twitter = require('twitter');

const Twit = new Twitter({
    consumer_key: 'XXX',
    consumer_secret: 'XXX',
    access_token_key: 'XXX',
    access_token_secret: 'XXX'
});

Twit.get('statuses/user_timeline', { screen_name: 'BBCSport', count: 1 }, function(error, tweets, response) {
    if (error) {
        console.log(error)
    }
    console.log(tweets);
});

I have tried console.log(tweets.text) which I assumed would work, however I receive an error saying that it is undefined.

I managed to solve this myself, I didn't realise that tweets was an array of objects, therefore couldn't be accesses with the .text notation, so the following solved it (looping around the array, and accessing the .text element on each one. Count is the count: 1 number, in the request.

for (var i = 0; i < count; i++) {
    console.log(tweets[i].text);
}
Owen Pauling
  • 11,349
  • 20
  • 53
  • 64
JBd
  • 115
  • 2
  • 9
  • `tweets` is an [array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array) of objects ā€“ Andreas Apr 08 '17 at 10:41
  • ok, so If i parse this into JSON, how would I then access the text element? ā€“ JBd Apr 08 '17 at 10:42

0 Answers0