简体   繁体   中英

how to call api inside loop using node js

I need to call API based on page number and on each and every page number API calling perform So I need to make API calling in loop based on page number.I uses request() for API Calling but when debug my code debug pointer is not going to block of response and not get response. My code is follow can any one guide me pls?

for (var i = 1; i <= totalpages; i++)
  request.get(
    "https://idms.dealersocket.com/api/account/getaccountlist?token=" +
      dealertoken +
      "&LayoutID=2002313&PageNumber=" +
      i +
      "&accounttype=i&accountstatus=a,c,b,o,r,s,x",
    (err, res) => {
      console.log(res);
    },
  );

I would suggest installing axios and do something like the following:

const axios = require('axios');

const sendGetRequest = async (url) => {
  const response = await axios.get(url);
  console.log(response);
}

for (var i = 1; i <= totalpages; i++) {
  const url = "https://idms.dealersocket.com/api/account/getaccountlist?token=" +
      dealertoken + "&LayoutID=2002313&PageNumber=" + i + "&accounttype=i&accountstatus=a,c,b,o,r,s,x"
  sendGetRequest(url);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM