6

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); 
    })
}
1
  • If you want to check the same file for each request, maybe it would be better to parse the data once and keep it in some variable, instead of re-reading the file over and over again? Commented Oct 17, 2019 at 11:38

2 Answers 2

31

You were very close. Just combine your 2 implementations into a third one:

async function getEmpDetailsByEmailIdAndUniqueCode(fileName,emailId,uniqueCode){
    console.log("inside getEmpDetailsByEmailIdAndUniqueCode()::"+emailId+"::"+uniqueCode);

    return new Promise(function(resolve,reject){
        var fetchData = [];
        fs.createReadStream(fileName)
            .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);
                resolve(fetchData);
            })
            .on('error', reject); 
    })
}

Note that i added fileName as first parameter, because i could not see it defined anywhere in your code.

I also did not test it, just glued together your code :).

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

Comments

3

Please use the code below

const getEmpDetailsByEmailIdAndUniqueCode = () => {
 return new Promise((resolve, reject) => {
  const fetchData = [];
let stream = fs.createReadStream(investorFileName1);
stream.on("error", err => reject(err));
stream.on('data', (row) => {
    if(row.unique_code != null && row.unique_code == uniqueCode && 
        row.investor_email_id == emailId) {
        fetchData.push(row);
        console.log(row);
    }
  })
stream.on("end", () => resolve(fetchdata));
 });
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.