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

3
  • so your top code, with the app.get... bits, is running on your server, in node.js? Commented Dec 17, 2020 at 9: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 developer.mozilla.org/fr/docs/Web/JavaScript/Reference/… Commented Dec 17, 2020 at 9: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! Commented Dec 17, 2020 at 9:17

1 Answer 1

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?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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