1

I have a terrible misunderstanding with promises in nodejs. I have a function in a module to convert CSV to JSON:

const URL_01 = `${__dirname}/URL/some_CSV.csv`;

const fs = require('fs'),
  Converter = require('csvtojson').Converter,
  converter = new Converter({}),

  convertToJson = function(file) {
    return new Promise( (res, rej) => {
      converter.on("end_parsed", (jsonData) => {
        if(!jsonData){
          rej("CSV to JSON conversion failed!")
        }
        res(jsonData);
      });
      fs.createReadStream(file).pipe(converter);
    });
  };

export default convertToJson;

Then I need to launch function convertToJson with CSVfile_URL in a server.js and save it to mongo db, now no data is output:

    app.get('/all', async (req, res) => {
  const data = await convertToJson( URL_01 );
  res.send(data);
});

Who could help me?

3 Answers 3

3

Couple issues:

  1. You're mixing ES2015 module syntax with CommonJS syntax which won't work.
  2. Doesn't look like you're using csvjson correctly based on the documentation for the module here.

With that said, using this answer, something like this may work (untested):

const fs = require('fs')
const csv = require('csvtojson')

const URL_01 = `${__dirname}/URL/some_CSV.csv`;

module.exports = file => new Promise((resolve, reject) => {
  const promises = []
  csv()
    .fromFile(URL_01)
    .on('json', converted => promsies.push(Promise.resolve(converted)))
    .on('done', error => {
      if (error) {
        reject(error)
        return
      }
    Promise.all(promises).then(convertedResults => resolve(convertedResults))
  })
})
Sign up to request clarification or add additional context in comments.

2 Comments

what if one is passing the file from client side to server? would i just put in the variable data inside fromFile?
No. fromFile takes a path to the location of the file on disk. If you're trying to process a file from client side upload, I think .fromStream() would be better.
0
const readStream = fs.createReadStream('yourFile.csv')

await new Promise((resolve, reject) => {
    csv(options)
        .fromStream(readStream)
        .subscribe(json=>{
            return new Promise((resolve, reject)=>{
                resolve(json)
            })
        })
        .on('header', header => {
            log(`Converting CSV to JSON - headers: ${headers}`)
        })
         .on('data', async data => {
                // data is a buffer object
                const jsonStr = data.toString('utf8')
                log(`Converting CSV to JSON - Current Line: ${jsonStr}`)
                await fs.appendFile(editDumb, jsonStr)
            })
        .on('done', () => {
            log('Finished converting CSV to JSON')
            resolve()
        })
        .on('error', e => {
            throw new BaseError(`Error while converting CSV to JSON - Error: ${e}`)
        })
})

Comments

-1

You may want to use a ready-made async-csv package from NPM. This will be as simple as:

const csv = require('async-csv');
let parsedObject = await csv.parse(csvFileAsString);

1 Comment

not working. I dont think async-csv can use await

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.