node-csv-parse icon indicating copy to clipboard operation
node-csv-parse copied to clipboard

Return error

Open omgcarry opened this issue 3 years ago • 5 comments

Hello. I'm trying to use parse

` const parse = require('csv-parse/lib/sync')

  const records = parse(event.target.result, {
    columns: true,
    skip_empty_lines: true
  })`

In general, it works fine. But if my file contains an issue with quotes I cannot handle this error Invalid Opening Quote: a quote is found inside a field at line 2 Is there a way how to handle this error? Need to show it in the custom

element

omgcarry avatar Jun 10 '21 19:06 omgcarry

Don't raise your hope too high but chances are that the relax will help you. It does not yet have its own documentation page, help is welcome, but I encourage you to look at its tests.

wdavidw avatar Jun 10 '21 21:06 wdavidw

I tried relax, but it didn't help :(

omgcarry avatar Jun 10 '21 21:06 omgcarry

trim helped me

metravonrech avatar Jun 11 '21 16:06 metravonrech

I also had this issue... while attempting to use the sync API in a native ES Modules package. Made my own Async API by promising the callback one. Similar to the csvtojson package's Async API.

const csvParse = (data, options) => new Promise((resolve, reject) => {
  csv.parse(data, options, (err, output) => err ? reject(err) : resolve(output))
})

// Usage:
async function main () {
  const result = await csvParse('name,email\nElon,[email protected]', { columns: true })
  console.log(result) 
}
main()

It's convenient and errors bubble up correctly. The whole point of async / await is to make async code look synchronous.

That said, not bubbling errors with the Sync API is definitely a bug.

Here's a sandbox with various scenarios involving CSV and ESM https://runkit.com/hesygolu/60cab0ebed19ef001a500480

FossPrime avatar Jun 16 '21 20:06 FossPrime

@rayfoss what you are proposing is not relevant to this issue. Worst, the code is not good. Just use csv-parse/lib/sync instead of using a useless promise since you don't need streaming. (and remove you comment, it creates confusion to users reading this issue)

wdavidw avatar Jun 16 '21 21:06 wdavidw