TypeError:无法读取未定义的属性“消息” - Twitter API [英] TypeError: Cannot read property 'message' of undefined - Twitter API

查看:138
本文介绍了TypeError:无法读取未定义的属性“消息” - Twitter API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是运行app.js时的输出。当一切正常时,这开始完全随机发生。绝对没有做出任何更改。

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.

更新:

使用 console.log('Try Favorite - ',err.message); 而不是 console.log(而不是 '尝试收藏 - ',错误[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);
        }


推荐答案

Twit 使用请求向Twitter API发送HTTP请求,因此错误将是单个对象而不是数组,因此您的错误处理程序将如下所示

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);
}

这篇关于TypeError:无法读取未定义的属性“消息” - Twitter API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆