本文介绍了Twitter lib出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Google Apps脚本中,我的项目中包含以下代码段以发送推文( 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库( MKvHYYdYA4G5JJHj7hxIcoh8V4pX7X1M_ ).).

尽管存在该错误,消息"test"实际上还是被发布了.有人知道如何解决吗?

解决方案

您好,这里是Twitter Lib的作者. @Mogsdad 在Twitter上指向我这里.我想我知道您的脚本发生了什么,这是Google脚本工作原理的一个特点.

您的大多数代码都在带有参数的函数中,然后在脚本的顶层调用该函数.当您转到运行"菜单并选择sendTweet函数时,会发生什么情况,即在执行所选函数之前会运行顶层脚本,并发送鸣叫当时带有测试"文本.

然后,sendTweet将不带任何参数运行,这意味着status变量为undefined.您正在向twit.sendTweet()发送未定义的值,从而导致看到的错误.

我在这里建议的只是将最后一行代码包装到一个函数中,以便您可以从运行"菜单中调用它,如下所示:

 function sendTestTweet() {
    sendTweet("test");
}
 

In Google Apps Script I have this snippet of code in my project to send a tweet (also in 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");

But the problem I'm having is that I get this error:

TypeError: Cannot read property "text" from undefined. (line 293, file "twitter", project "Twitter lib")

Line 293 is from version 21 of the "Twitter lib" library (MKvHYYdYA4G5JJHj7hxIcoh8V4oX7X1M_).

The message "test" actually gets tweeted, despite that error. Does anyone know how to fix it?

解决方案

Hi, author of Twitter Lib here. @Mogsdad pointed me here over Twitter. I think I know what's going on with your script, and it's a peculiarity of how Google Script works.

You have most of your code in a function that takes an argument, and then you have a call to the function at the top level of your script. What happens, when you go to the "Run" menu and select your sendTweet function, is that the script at the top level gets run before the selected function is executed, and the tweet would be sent at that time with the "test" text.

Then after that, sendTweet gets run with no arguments, meaning the status variable is undefined. You're sending an undefined value into twit.sendTweet(), causing the error you see.

What I'd recommend here is simply wrapping your last line of code into a function so you can call it from the Run menu, like this:

function sendTestTweet() {
    sendTweet("test");
}

这篇关于Twitter lib出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 04:25