I have node js service which reads csv file and and filter records based on parameters provided. I need to wait till file reading is processed.but its not waiting. I tried using async/await and promise but no luck.
How to wait till file is processed and return json response.
app.get('/getEmpInfo/:emailId/:unique_code', async (req,res) => {
console.log("req.params.emailId :"+req.params.emailId);
console.log("req.params.unique_code :"+req.params.unique_code);
const response = await getEmpDetailsByEmailIdAndUniqueCode(req.params.emailId,req.params.unique_code);
res.send(response)
});
async function getEmpDetailsByEmailIdAndUniqueCode(emailId,uniqueCode){
console.log("inside getEmpDetailsByEmailIdAndUniqueCode()::"+emailId+"::"+uniqueCode);
var fetchData = [];
// try 1
/* await fs.createReadStream(investorFileName1)
.pipe(csv())
.on('data', (row) => {
if(row.unique_code != null && row.unique_code == uniqueCode && row.investor_email_id == emailId) {
fetchData.push(row);
console.log(row);
}
})
.on('end', () => {
console.log('CSV file successfully processed');
console.log(fetchData);
}); */
// try 2
var response = fs.createReadStream(fileName).pipe(csv());
return new Promise(function(resolve,reject){
response.on('end', () => resolve(fetchData));
response.on('error', reject);
})
}