1

How would I go about chaining an http.request within the response of another http.request and then push them into an array before going to the front end?

router.get("/:team", (req, res) => {


  let teamParams = teams[req.params.team];
  twitter.get("search/tweets", teamParams, (err, data, resp) => {
    let tweetArr = [];
    let text = data.statuses;
    text.map((dat) => {
      let im = dat.entities.urls[0].url
      dat.links = im;
      tweetArr.push(dat);
    });
  

   res.json({ message: "Success", tweets: tweetArr });
  });
});

Currently I get my data object loop through it and add a url as a property. Now I want to chain another http request to make an API call to another API and get a response, before I use res.json. I've tried a workaround with promises but I can never return the full object with the response from the second api call.

This is what I have so far, I have managed to get to a point where my object contains the requests from the second link. How can I return all the tweets into an array I can finally resolve?

require("dotenv").config();
const Twitter = require("twitter");
const API_IMAGE_PREV = "http://api.linkpreview.net/";
const request = require("request");
const key = process.env.IM_PREV_KEY;

let twitter = new Twitter({
  consumer_key: process.env.TWITTER_CONSUMER_KEY,
  consumer_secret: process.env.TWITTER_CONSUMER_SECRET,
  bearer_token: process.env.TWITTER_BEARER_TOKEN
});
let teamParams = {
  q: "from:ManUtdMEN MUFC",
  count: 2,
  result_type: "recent"
};


var third = function thirdUrl(next) {
  var promise = new Promise(function(resolve, reject) {
    next.forEach(x => {
      let ln = x.links;
      const options = {
        url: "http://api.linkpreview.net/?key=" + key + "&q=" + ln,
        method: "get"
      };
      request.get(options, (err, req, res) => {
        if (err) {
          console.log(err);
        } else {
          x.desc = res;
        }
      });
    });
  });
};

var second = function secondUrl(previous) {
  var promise = new Promise(function(resolve, reject) {
    let p = previous.statuses;
    let links = [];
    p.forEach(t => {
      let l = t.entities.urls[0].url;
      t.links = l;
    });

    resolve(p);
  });
  return promise;
};

twitter
  .get("search/tweets", teamParams)
  .then(second)
  .then(third)
  .catch(function(error) {
    throw error;
  });

5
  • What other requests are you looking to make? Presumably one per URL being fetched out each Tweet? Commented Jul 9, 2018 at 22:09
  • Yup just the one for each tweet, i’m sending it through to another API to generate a preview Commented Jul 9, 2018 at 22:20
  • Ok so after we've fetched info from each tweet, what goes into the response to the client? This should be fairly trivial to write using async / await Commented Jul 9, 2018 at 22:23
  • Well the res.json at the moment just sends the full tweet object plus the added link that I want to make the subsequent request on, but then once angular picks it up for the time being I just have card with the tweet ID created date the text and that link. Commented Jul 9, 2018 at 22:27
  • The second API I want to pass that dat.link to, will follow the link and return an object with the title, a short description, a url and an image. So ideally I would present my user with the image A title, description and the user’s tweet Commented Jul 9, 2018 at 22:29

1 Answer 1

1

What module are you using for the http requests? Here's an example with axios.

const axios = require('axios');

router.get("/:team", (req, res) => {
  let teamParams = teams[req.params.team];
  twitter.get("search/tweets", teamParams, async (err, data, resp) => {
    let tweetArr = [];
    let text = data.statuses;
    text.map((dat) => {
      let im = dat.entities.urls[0].url
      dat.links = im;
      tweetArr.push(dat);
    });
    let res = await Promise.all(tweetArr.map(dat => axios.get(dat.links));
    res.json({ message: "Success", tweets: res });
  });
})
Sign up to request clarification or add additional context in comments.

4 Comments

Was just using the requests library and express Js, but I could try axios. I will let you know how it goes.
Then you need to promisify request as it does not return promises by default
getting this error, ReferenceError: tweetArr is not defined
is that because I need to promisify first?

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.