0

I have the code below that runs fine when run in a standalone file. But when I try to use it in my API endpoint and send a request with the postman, it can't seem to work.

I can't understand why. I also have the same issue when trying to write an excel file with the same API endpoint. If I specify a path, it won't find it. If I just use the filename, it will write fine in the current directory.

What am I missing? the code below is from when I use it in a standalone file. If I use it inside this route below, it won't work.

exports.download_excel = async (req, res) => {....

full code below

// modules import
const path = require('path');
const fs = require('fs');
const util = require('util');
const csv = require('@fast-csv/parse');
const { forEach } = require('p-iteration');

// const path = '../csv/';
const casesBO = [];


const readCSVFiles = async function() {
  try {
    const allLocalFiles = path.join('csv/');
    const readDir = util.promisify(fs.readdir);
    await readDir(allLocalFiles, async function(err, file) {
      forEach(file, async item => {
        fs.createReadStream('csv/' + item)
          .pipe(csv.parse({ headers: true, delimiter: ';' }))
          .on('error', error => console.error(error))
          .on('data', async row => {
            if (row['[REGION2]'] !== 'FR') {
              casesBO.push(row['[CALLERNO_EMAIL_SOCIAL]']);
              console.log(
                `${row['[AGENT]']} is ${row['[REGION2]']} and case = ${
                  row['[CALLERNO_EMAIL_SOCIAL]']
                }`
              );
            }
          })
          .on('end', async rowCount => {});
      });
    });
  } catch (error) {
    console.log(error);
  }
};
0

1 Answer 1

1

You are awaiting readDir, but you are also giving it a callback function. You can't both await a Promise and also give it a callback. For that matter, Promises don't take a callback as argument at all.

Also you are writing async everywhere, this is useless for functions that don't await anything inside.

const readCSVFiles = async function () {
    try {
        const allLocalFiles = path.join('csv/');
        const readDir = util.promisify(fs.readdir);

        const files = await readDir(allLocalFiles);

        for (let file of files) {

            fs.createReadStream('csv/' + file)
                .pipe(csv.parse({ headers: true, delimiter: ';' }))
                .on('error', error => console.error(error))
                .on('data', row => {
                    if (row['[REGION2]'] !== 'FR') {
                        casesBO.push(row['[CALLERNO_EMAIL_SOCIAL]']);
                        console.log(
                            `${row['[AGENT]']} is ${row['[REGION2]']} and case = ${row['[CALLERNO_EMAIL_SOCIAL]']
                            }`
                        );
                    }
                })
                .on('end', rowCount => { });
        }
    } catch (error) {
        console.log(error);
    }
};
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I'm still new at programming and your answer helped me a lot. I had figured out that i needed to add ___dirname, before the path for it to work but then it seemed that everything would stall when i tried to run it. With your code everything worked smoothly.

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.