与Twitter库出现错误

4
在Google Apps Script中,我有这段代码片段来发送推文(也可在jsBin中查看):
function sendTweet(status) {

  var twitterKeys= {
    TWITTER_CONSUMER_KEY: "x",
    TWITTER_CONSUMER_SECRET: "x",
    TWITTER_ACCESS_TOKEN: "x",
    TWITTER_ACCESS_SECRET: "x"    
  };

  var props = PropertiesService.getScriptProperties();

  props.setProperties(twitterKeys);
  twit = new Twitter.OAuth(props);

  var service = new Twitter.OAuth(props);


  if ( service.hasAccess() ) {

    var response = twit.sendTweet(status);

    if (response) {

      Logger.log("Tweet ID " + response.id_str);

    } else {

      // Tweet could not be sent
      // Go to View -> Logs to see the error message

    }
  }  
}

sendTweet("test");

但是我遇到的问题是出现了这个错误:
TypeError: Cannot read property "text" from undefined. (line 293, file "twitter", project "Twitter lib")

第293行来自"Twitter lib"库的21版本(MKvHYYdYA4G5JJHj7hxIcoh8V4oX7X1M_)。

尽管出现错误,实际上“test”消息已经发布到推特上了。有人知道如何解决吗?

2个回答

2
你好,Twitter Lib 的作者在这里。@Mogsdad 在 Twitter 上指引我来到这里。我想我知道你的脚本出了什么问题,这是 Google Script 工作方式的一种特殊性。
你的大部分代码都在一个带有参数的函数中,然后你在脚本的顶层调用了该函数。当你进入“运行”菜单并选择你的 sendTweet 函数时,会先执行顶层脚本,此时推文将使用“test”文本发送。
然后,在没有参数的情况下运行 sendTweet,这意味着 status 变量为 undefined。你将一个未定义的值传递给 twit.sendTweet(),导致你看到的错误。
我建议你将最后一行代码简单地封装成一个函数,这样你就可以从“运行”菜单中调用它,就像这样:
function sendTestTweet() {
    sendTweet("test");
}

如果你有时间查看我在这里关于Twitter lib的问题,我会非常感激。http://stackoverflow.com/questions/35944948/what-parameters-will-twitter-libs-fetchtweets-method-accept - MAK Design Labs

1

简单回顾一下,你看到的错误是:

类型错误:无法从未定义的对象中读取“text”属性。(第293行,文件“twitter”,项目“Twitter lib”)

这是在sendTweet()方法中发生的,请参见以下内容。

/**
* Upload a tweet to Twitter with optional media.
*
* @param {string | Tweet} tweet the status text to send as a Twitter update
* @param {optional object} params any additional parameters to send as part of the update post
* @return {object} the Twitter response as an object if successful, null otherwise
*/
OAuth.prototype.sendTweet = function(tweet, params) {
  var i;
  var payload = {       //<=== 293
    "status" : (tweet.text || tweet)
  };

您的代码使用一个字符串参数status调用了这个方法,该参数被设置为"test"。

库的作者允许tweet参数是以下两种情况之一:

  • 它可以是一个包含要推送的消息的text属性的对象,或者
  • 它可以是一个字符串。

然而,处理方式首先检查tweet.text,然后如果不存在,则检查一个字符串tweet。当tweet.text不存在时(即仅使用字符串时),会抛出TypeError。

我已经联系了库的作者,让他们发布修复程序。但是,在此期间,您可以发送一个带有text属性的Tweet对象,或者复制该库并自行更新它。

  • Send Tweet object. The Tweet object is documented in the Twitter API v1.1 documentation, but since the only property involved in this operation is text, a simple change in your status function will do the trick. Just ensure that status is an object with a text property.

    function sendTweet(status) {
      if (typeof status === string)
        status = {text:status};
      ...
    
  • Update the library yourself. To avoid the error, and handle the parameter options properly, line 294 in the library should be:

    "status" : (tweet.hasOwnProperty("text") ? tweet.text : tweet)
    

    Or:

    "status" : (typeof tweet === 'object' ? tweet.text : tweet)
    

    You'll need to publish it, and update the library ID in your code, but that should take care of this problem. Once a library update is available, you can switch back.


@user4411473 谢谢,但不是 - 那是阿米特的,他不是这个库的作者。 - Mogsdad
需要注意的是:如果 status 是 null 或 undefined,hasOwnProperty 也会触发运行时 TypeError,而 typeof null"object",所以你最后一行代码不会捕获到 null。JavaScript 不是很有趣吗? - air_hadoken

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接