I am trying to get data from multiple CSV files using 'csvtojson'.
- Acquired the array of file names in a directory.
- Ran forEach to get the data from different CSV files in JSON and pushed into a single array.
The problem is I cant send data to the client because promises take longer to fetch data and I am unable to figure out a way.
Heres my code :
router.get("/covidData/", (req, res) => {
const jsonData = [];
const files = fs.readdirSync(__dirname.replace("routes", "public/covidData"));
const fetchData = () => {
files.forEach(async (file) => {
let response = await csvtojson({
includeColumns: /Date Announced|Detected State|Current Status/,
}).fromFile(__dirname.replace("routes", "public/covidData/" + file));
jsonData.push(...response);
});
};
fetchData();
res.send(jsonData);
});