1

I want to send two API data to the HTML file using Axios But I can not figure out the problem please help me with that My code is

app.get('/', (req, res) => {
    const Url1 = "https://api.covidindiatracker.com/state_data.json";
    const x = axios.get(Url1).then((response1) => {
        res.render("index", {
          appName: "COVID-19 Tracker",
          pageName: "India Fights Corona",
          data1: response1.data
      });
    })
});

app.get('/', (req, res) => {
  const Url2 = "https://api.covidindiatracker.com/total.json";
  const y = axios.get(Url2).then((response2) => {
      res.render("index", {
        data2: response2.data
      });
    })
});

in my HTML page

<h3><%= JSON.stringify(data1.recovered) %></h3>

and 

<td><%= data2.id %></td>

Why it not work please help me

  • so your top code, with the `app.get...` bits, is running on your server, in node.js? – TKoL Dec 17 '20 at 09:13
  • from what I read your first path handler will take priority over the second (or the other way I can't remember) If you want to make your client request handling depending on two subsequent requests, declare one handler and have a look at Promise.all https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/Promise/all – 36ve Dec 17 '20 at 09:15
  • you are sending a response in your first app.get and that completes your request-response cycle. Therefore your second app.get never get's a chance to execute! – dhruw lalan Dec 17 '20 at 09:17

1 Answers1

1
app.get('/', (req, res) => {
  const url1 = "https://api.covidindiatracker.com/state_data.json";
  const Url2 = "https://api.covidindiatracker.com/total.json";
  axios.all([
   axios.get(url1), 
   axios.get(url2)
 ]).then(axios.spread((data1, data2) => {
     res.render("index", appName: "COVID-19 Tracker",
          pageName: "India Fights Corona",
          data1: data1.data,
          data2: data2.data
 }));
});

This Link will be helpful

How to post multiple Axios requests at the same time?

sourav satyam
  • 980
  • 5
  • 11