ryu1kn / csv-writer

Convert objects/arrays into a CSV string or write them into a CSV file
https://www.npmjs.com/package/csv-writer
MIT License
246 stars 39 forks source link

[Bug]: Cannot Read Properties of undefined (reading 'map') when headers aren't provided #102

Open ondrovic opened 1 year ago

ondrovic commented 1 year ago

Headers are optional but in certain instances throws the following error

TypeError: Cannot read properties of undefined (reading 'map')
    at ObjectCsvStringifier.getRecordAsArray (C:\Users\ondro\source\git\test-csv-writer\node_modules\csv-writer\src\lib\csv-stringifiers\object.ts:21:30)
    at C:\Users\ondro\source\git\test-csv-writer\node_modules\csv-writer\src\lib\csv-stringifiers\abstract.ts:20:77
    at Function.from (<anonymous>)
    at ObjectCsvStringifier.CsvStringifier.stringifyRecords (C:\Users\ondro\source\git\test-csv-writer\node_modules\csv-writer\src\lib\csv-stringifiers\abstract.ts:20:32)
    at CsvWriter.<anonymous> (C:\Users\ondro\source\git\test-csv-writer\node_modules\csv-writer\src\lib\csv-writer.ts:17:51)
    at step (C:\Users\ondro\source\git\test-csv-writer\node_modules\csv-writer\dist\lib\csv-writer.js:33:23)
    at Object.next (C:\Users\ondro\source\git\test-csv-writer\node_modules\csv-writer\dist\lib\csv-writer.js:14:53)
    at C:\Users\ondro\source\git\test-csv-writer\node_modules\csv-writer\dist\lib\csv-writer.js:8:71
    at new Promise (<anonymous>)
    at __awaiter (C:\Users\ondro\source\git\test-csv-writer\node_modules\csv-writer\dist\lib\csv-writer.js:4:12)

Code Sandbox Here, if not sure if it's a bug in my setup or something else, but would be nice to not have to include a header.

RomanticGirl commented 11 months ago
interface ObjectCsvWriterParams {
    path: string;
    header: ObjectStringifierHeader;
    fieldDelimiter?: string;
    recordDelimiter?: string;
    headerIdDelimiter?: string;
    alwaysQuote?: boolean;
    encoding?: string;
    append?: boolean;
}

use another API with optional header. It's not a bug.

interface ArrayCsvWriterParams {
    path: string;
    header?: string[];
    fieldDelimiter?: string;
    recordDelimiter?: string;
    alwaysQuote?: boolean;
    encoding?: string;
    append?: boolean;
}