Why am I getting an error using tweet() in @velo/twitter-integration?

I would like to post tweets programatically from my site’s backend. I have installed the twitter-integration (v1.9.0) npm package, have followed the steps in the README and have also checked out the demo site, which works nicely.

I am currently trying to test posting tweets to a Twitter profile with read and post permissions enabled in the Twitter developer dashboard. I am on the Standard stream.

The following code is not working when executing it with the debug tool in the backend:

export async function generateTweetAndPost() {
 try {
 await tweet("hello world");

 } catch (err) {
        console.log(err)
 }
}

The caught error is [“Cannot read property ‘0’ of undefined”] . I initially thought this was an issue with the object I have added to the Secrets Managers, which would return an error at getTwitterClient(), however, I subsequently tested getLatestTweet(username) (which also calls getTwitterClient()) and this is working as expected, so I believe I’ve excluded an error from the secrets object.

I’m pretty stumped. Obviously the code in twitter.js that I’ve installed is identical to the code in the demo site that is working, so I have absolutely no idea what’s going wrong! I would be grateful if someone could shed some light on this.

Thanks in advance!

When you say, “code not working when ex. with debug tool.” ;

  1. Do you want solutions that help you debug?
  2. Do you want a solution how to “get latest tweets”, and subscribe to “twitter api”, Then attain a user scope which has permissions to then getClient classes?

Is that all the code you have?

Here is a start: twitter-wrapper.jsw

import { tweet, getLatestTweet } from ‘@velo/twitter-integration-backend’ ;

export async function tweetWrapper(content) {
try {
return await tweet(content);
} catch (err) {
console.error(err);
return Promise .reject( Couldn't tweet. Contact site owner for more info. );
}
}

const DEFAULT_ACCOUNT = ‘@VeloExamples’ ;
export async function getLatestTweetWrapper(accountNameInput) {
const accountName = accountNameInput || DEFAULT_ACCOUNT ;
try {
const res = await getLatestTweet(accountName);
return res;
} catch (err) {
console.error(err);
return Promise .reject( Couldn't get the latest tweet from the account: ${accountName} . Contact site owner for more info. );
}
}

HOME

import { tweetWrapper, getLatestTweetWrapper } from ‘backend/twitter-wrapper.jsw’ ;

const SUCCESS_MSG = Success! Just tweeted the ;
const CONTENT_PREFIX = timestamp: ;

$w.onReady(function () {
$w( ‘#tweetBtn’ ).onClick(generateTweetAndPost);
$w( ‘#getTweetBtn’ ).onClick(getTweetAndShow);
});

async function generateTweetAndPost() {
$w( ‘#loader’ ).expand();
try {
const content = createTweetContent();
const res = await tweetWrapper(content);
$w( ‘#tweetBtn’ ).disable();
$w( ‘#tweetTxt’ ).text = SUCCESS_MSG + ’ ’ + res;
$w( ‘#tweetTxt’ ).show();
$w( ‘#accountLinkBtn’ ).show();
} catch (err) {
$w( ‘#tweetBtn’ ).disable();
$w( ‘#tweetBtn’ ).label = err;
}
$w( ‘#loader’ ).collapse();
}

function createTweetContent() {
const dateInstance = new Date ();
const date = dateInstance.toLocaleDateString();
const time = dateInstance.toLocaleTimeString();
const content = CONTENT_PREFIX + ’ ’ + date + ’ ’ + time;
return content;
}

async function getTweetAndShow() {
const accountNameInput = $w( ‘#accountNameInput’ ).value;
$w( ‘#loader’ ).expand();
try {
const res = await getLatestTweetWrapper(accountNameInput);
$w( ‘#lastTweetTxt’ ).value = res;
} catch (err) {
$w( ‘#lastTweetTxt’ ).value = err;
}
$w( ‘#loader’ ).collapse();
disableUntilNewValue();
}

function disableUntilNewValue() {
const getTweetBtn = $w( ‘#getTweetBtn’ );
getTweetBtn.disable();
$w( ‘#accountNameInput’ ).onKeyPress(getTweetBtn.enable);
}

Hi Gavin, thanks for your reply. I simply want my tweet() to work. I’ve tried your tweetWrapped code, passing “Hello world” as the content. It returns the same error, but also the rejected promise message `Couldn’t tweet. Contact site owner for more info.’

I don’t intend to use getLatestTweet, I merely tried it in an attempt to isolate the issue. For example, in my sites’s backend, this code returns my latest tweet (or retweet in this instance):

export async function getTweet() {
 try {
 let latestTweet = await getLatestTweet("rynmrgn");
        console.log(latestTweet);

 } catch (err) {
        console.log(err)
 }
}

I can’t fathom where the [“Cannot read property ‘0’ of undefined”] is caught from in my tweet() code. It is this code that I need to work.

If I don’t catch the error in my backend, I get this in console:

(node:1) UnhandledPromiseRejectionWarning: TypeError: Cannot read property ‘0’ of undefined at tweet (/dynamic-modules/third-party-npm/facf9043/WC_BEGIN/@velo/twitter-integration-backend/1.9.0/WC_END/@velo/twitter-integration-backend/backend/twitter.js:23:74) (Use node --trace-warnings ... to show where the warning was created)

Is that not an error in the tweet.js code that’s included with the npm package?

@ryanmorgan12 So everything sounds like its an object/parse error. So everything is basically correct but there seems to be a small fuss from the api the code chunk isnt passing a data chunk the way they want. I get this in php / js so its not translating a data object from url parse to object to json object to string. Something in there. Maybe the $w object.

Run node from the directory file in question. Then use the trace flag. And run the error. Find your log file/debug log and the error location will show. Open the code to the line of code and post the code that flags the error. Make sure that isnt the flag chunk. Make sure its the code folding part which then calls the flag. Your error happens here and the code will show what its asking for that its not getting.

It is in the twitter.js node_module directory. Youll want the code section that is invoke and run. The function in there which calls flag module is the function which your not passing what it wants but it is getting something. So find out what its getting and change the pass data. Its likely a json array object