TypeError: newData.charCodeAt is not a function
Ankur-Jat opened this issue · comments
I was using below snippet.
const UTIL = require('util');
if (process.argv.length !== 3) {
UTIL.error("Invalid file. Please pass a vlaid file path");
process.exit(1);
}
const CSVREADER = require('csv-reader');
const FS = require('fs');
const READ_STREAM = FS.createReadStream(process.argv[2]);
READ_STREAM
.pipe(CSVREADER({parseNumbers: true, skipEmptyLines: true, skipHeader: true}))
.on('data', function(row) {
UTIL.log('Row arived', row);
})
.on('end', function() {
UTIL.log('File parsing is done');
})
.on('error', function() {
UTIL.error('CSV read error:', error);
});
While running this with node fixPCUEntries.js ./updatablePCU.csv
getting below error
/home/ankurkumar/workspace/self-learning/csv-parsing/node_modules/csv-reader/index.js:105
if (newData.charCodeAt(0) === 0xfeff) {
^
TypeError: newData.charCodeAt is not a function
at CsvReadableStream._processChunk (/home/ankurkumar/workspace/self-learning/csv-parsing/node_modules/csv-reader/index.js:105:25)
at CsvReadableStream._transform (/home/ankurkumar/workspace/self-learning/csv-parsing/node_modules/csv-reader/index.js:249:10)
at CsvReadableStream.Transform._read (_stream_transform.js:186:10)
at CsvReadableStream.Transform._write (_stream_transform.js:174:12)
at doWrite (_stream_writable.js:397:12)
at writeOrBuffer (_stream_writable.js:383:5)
at CsvReadableStream.Writable.write (_stream_writable.js:290:11)
at ReadStream.ondata (_stream_readable.js:639:20)
at emitOne (events.js:116:13)
at ReadStream.emit (events.js:211:7)
Try Fs.createReadStream(process.argv[2], { encoding: 'utf8' });
You passed a binary stream, while it expects a textual stream.
If you need autodetection of stream encoding, you can pipe a binary stream through autodetect-decoder-stream
, like in the README.