node-csv-parse
node-csv-parse copied to clipboard
Return error
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
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.
I tried relax
, but it didn't help :(
trim
helped me
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
@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)