Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Copilot Lvl 3
Message 1 of 6

Can anyone help to understand JavaScript and Twitter api

Hello, I'm trying to create a twitter bot for fun but I don't understant the logic of JavaScript with the twitter API.

 

function searchTweet()
{
  var params = { q: 'cat'};
  var retweetId;
  T.get('search/tweets', params, (err, data, response) => 
  {
    retweetId = data.statuses[0].id_str;
  })
  return retweetId;
}

console.log(searchTweet());

The return is done before the T.get(..) and I don't know why . the problem is that return undefined and not the ID.

If you can explain me it would be very nice.

 

5 Replies
Highlighted
Commander Lvl 1
Message 2 of 6

Re: Can anyone help to understand JavaScript and Twitter api

Hey @Lancelot-d, just to make sure I understand the issue correctly, you are saying that the return statement after the

T.get 

  gives an error since 

retweetId

 is undefined?  



*Mark helpful posts with Accept as Solution to help other users locate important info. Don't forget to give Kudos for great content!*
Highlighted
Copilot Lvl 3
Message 3 of 6

Re: Can anyone help to understand JavaScript and Twitter api

Yes exactly                                 .

Highlighted
Commander Lvl 2
Message 4 of 6

Re: Can anyone help to understand JavaScript and Twitter api

Break it down into smaller steps. For instance, what happens when you use this:

 

T.get('search/tweets', params, function(error, tweets, response) {
  if (!error) {
    console.log(tweets);
    retweetId = tweets.statuses[0].id_str;
    console.log(retweetid);
  }
else {
console.log("error!");
} });

Do you get tweets, or error?

Please follow-up to let us know how you made out. For good karma, mark a reply as the answer if it helped!

Highlighted
Copilot Lvl 3
Message 5 of 6

Re: Can anyone help to understand JavaScript and Twitter api

function searchTweet()
{
  var retweetId;
  var params = { q: 'cat'};
  T.get('search/tweets', params, function(error, tweets, response) {
    if (!error) {
      console.log(tweets);
      retweetId = tweets.statuses[0].id_str;
      console.log(retweetId);
    }
    else {
      console.log("error!");
    }
  });
}

Yes, it works but how can I take the ID out of the function T.get because T.get is in searchTweet so I'm a little bit confused

Highlighted
Commander Lvl 2
Message 6 of 6

Re: Can anyone help to understand JavaScript and Twitter api

All right, we're pushing the limits of my Javascript ability here, but here's a guess.

 

First, you need to tell us which NPM library you are using. Assuming it's the "twitter" package,

then I believe it's an async library. I think this means you need to use async and await, e.g.:

 

aysnc function searchTweet()
{
...
await T.get(...) {
...
}
return retweetId;
}

The twitter NPM is out-of-date and not maintained. If that is the library you are using, there are

other, newer packages out there (see).

 

Please follow-up to let us know how you made out. For good karma, mark a reply as the answer if it helped!