问题
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?
回答1:
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");
}
回答2:
Just to recap, the error you've seen is:
TypeError: Cannot read property "text" from undefined. (line 293, file "twitter", project "Twitter lib")
That is in the sendTweet()
method of the library, see below.
/**
* 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)
};
Your code invokes this method with a single string parameter, status
, which is set to "test".
The author of the library allowed for the tweet
parameter to be one of two things:
- It can be an object with a
text
property containing the message to Tweet, or - It can be a string.
However, the way that's being handled checks for tweet.text
first, then if that does not exist it checks for a string tweet
. When tweet.text
does not exist (i.e. when using just a string), that TypeError is thrown.
I've reached out to the library author so they can publish the fix. However, in the meantime you can send a Tweet
object with a text
property, or take a copy of the library and update it yourself.
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 yourstatus
function will do the trick. Just ensure thatstatus
is an object with atext
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.
来源:https://stackoverflow.com/questions/34668592/getting-an-error-with-twitter-lib