0

I'm trying to send a response from the Twitter API to the Firebase HTTP response - abit confused by the scope in Node. I'm getting a console.log on the firebase server so everything is set up fine with the Twitter API. I can't figure out how to pass the response from inside the GET req to a external variable, which intail will send the Firebase HTTP response.


exports.twitterFunction = functions.https.onRequest((request, response) => {

  const client = new Twitter({
    consumer_key: "",
    consumer_secret: "",
    access_token_key: "",
    access_token_secret: ""
  });

  const params = { screen_name: "" };
  const latesttweet = "";

  client.get("statuses/user_timeline", params, (error, tweets, response) => {
    if (error) throw error;
    this.latesttweet = tweets[0].text;
    console.log(tweets[0].text);
    console.log(latesttweet);
  });

  response.send(latesttweet);

});

1 Answer 1

2

client.get() is asynchronous and returns immediately, before the data is available. The data will be available some time later in the callback function that you passed to it. Therefore, you should be sending the response from inside the callback, since that's where your data will be available.

  client.get("statuses/user_timeline", params, (error, tweets, response) => {
    if (error) throw error;
    this.latesttweet = tweets[0].text;
    console.log(tweets[0].text);
    console.log(latesttweet);
    response.send(latesttweet);   // move this inside the callback
  });
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Doug - knew it would be something simple!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.