I am trying to make axios POST requests inside a for loop. However, the axios post POST request is being run n times with only the last value of the loop. Running twice with value sheetName="Virudhunagar Beds"
. Below is my piece of code:
const axios = require('axios')
const sheetDistrictMap = {
"Ariyalur Beds": "5ea0abd3d43ec2250a483a4f", "Virudhunagar Beds": "5ea0abd3d43ec2250a483a58"
}
let promises = [];
for (sheetName in sheetDistrictMap) {
promises.push(
axios.post('https://tncovidbeds.tnega.org/api/hospitals', {
District: sheetDistrictMap[sheetName],
FacilityTypes: ["CHO", "CHC", "CCC"],
IsGovernmentHospital: true,
IsPrivateHospital: true,
pageLimit: 100
})
.then(res => {
var outputJsonArray = [];
for (i = 0; i < res.data.result.length; i++) {
var rowJson = {};
var rowdata = res.data.result[i];
rowJson["Name"] = rowdata["Name"];
outputJsonArray.push(rowJson);
}
console.log("Parsed sheet: " + sheetName);
console.log(outputJsonArray);
console.log("========================================================");
})
.catch(error => {
console.error(error)
})
)
}
Promise.all(promises).then(() => console.log("All data scraped"));
How do I make async calls with each loop parameter?