Search code examples
javascriptnode.jstwitterbotsnode-modules

TypeError: Cannot read property 'message' of undefined - Twitter API


Below is the output when running app.js. This started occurring totally at random when everything was working fine. Absolutely no changes were made.

 TypeError: Cannot read property 'message' of undefined
        at /home/ec2-user/environment/rt-bot/app.js:78:48
        at Request._callback (/home/ec2-user/environment/node_modules/twitter/lib/twitter.js:220:14)
        at Request.self.callback (/home/ec2-user/environment/node_modules/request/request.js:186:22)
        at emitTwo (events.js:106:13)
        at Request.emit (events.js:191:7)
        at Request.<anonymous> (/home/ec2-user/environment/node_modules/request/request.js:1163:10)
        at emitOne (events.js:96:13)
        at Request.emit (events.js:188:7)
        at IncomingMessage.<anonymous> (/home/ec2-user/environment/node_modules/request/request.js:1085:12)
        at IncomingMessage.g (events.js:292:16)

I've tried creating new instances from my master branch (with no commits since last working) and even still getting this error. Any ideas?

The code causing the error, though this code was working previously.

T.get('search/tweets', query, function(err, data, response) {
  // continue if no errors
  if(!err){
    // loop
    for(let i = 0; i < data.statuses.length; i++){
      // get latest tweet ID
      let id = { id: data.statuses[i].id_str }

      // try favorite
      T.post('favorites/create', id, function(err, response){

        // log failures
        if(err){
          console.log('Try Favorite - ', err[0].message);
        }

        // log success
        else{
          let username = response.user.screen_name;
          let tweetId = response.id_str;
          console.log('Favorited: ', `https://twitter.com/${username}/status/${tweetId}`)
        }

      });

Image of first occurrence. As you can see everything was functioning fine, then this error came out of nowhere with absolutely no changes to environment or codebase.

As you can see everything was functioning fine, then this error came out of nowhere with absolutely no changes to environment or codebase.

UPDATE:

Output when logging error with console.log('Try Favorite - ', err.message); rather than console.log('Try Favorite - ', err[0].message);

[[Apr 28 21:27:00.702]] [LOG]   Try Favorite -  HTTP Error: 429 Too Many Requests
[[Apr 28 21:27:00.705]] [LOG]   Try Favorite -  HTTP Error: 429 Too Many Requests
[[Apr 28 21:27:00.706]] [LOG]   Try Favorite -  HTTP Error: 429 Too Many Requests
[[Apr 28 21:27:00.707]] [LOG]   Try Favorite -  HTTP Error: 429 Too Many Requests
[[Apr 28 21:27:00.708]] [LOG]   Try Favorite -  HTTP Error: 429 Too Many Requests
[[Apr 28 21:27:00.709]] [LOG]   Try Favorite -  HTTP Error: 429 Too Many Requests
[[Apr 28 21:27:00.712]] [LOG]   Try Favorite -  HTTP Error: 429 Too Many Requests
[[Apr 28 21:27:00.713]] [LOG]   Try Favorite -  HTTP Error: 429 Too Many Requests
[[Apr 28 21:27:00.718]] [LOG]   Try Favorite -  HTTP Error: 429 Too Many Requests
[[Apr 28 21:27:00.793]] [LOG]   Try Favorite -  HTTP Error: 429 Too Many Requests

Also note that the retweet function uses the same console logging method. See code below, and outputs fine.

// try retweet
      T.post('statuses/retweet', id, function(err, response){

        // log failures
        if(err){
          console.log('Try Retweet - ', err[0].message);
        }

Solution

  • Twit uses request to send HTTP requests to Twitter API, so the error will be single object not an array, so your error handler will look like

    if(err){
      console.log('Try Favorite - ', err.message);
    }