Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

417
Views
TypeError [ERR_INVALID_ARG_TYPE] con canalización de nodo

Estoy usando el nodo 18.7 en ubuntu. Estoy tratando de analizar un montón de archivos csv en objetos (usando csv-parse), en última instancia, para cargarlos en una base de datos. Debido a que hay una gran cantidad de estos, decidí probar las transmisiones y me gustaría usar el estilo de espera asíncrona.

Basado en el uso de la sintaxis async/await con el flujo de nodos , he cambiado mi código a

 const { parse } = require('csv-parse'); const path = __dirname + '/file1.csv'; const opt = { columns: true, relax_column_count: true, skip_empty_lines: true, skip_records_with_error: true }; console.log(path); const { pipeline } = require('node:stream/promises'); async function readByLine(path, opt) { const readFileStream = fs.createReadStream(path); const writeFileStream = fs.createWriteStream(__dirname + '/file2'); var csvParser = parse(opt, function (err, records) { if (err) throw err; }); await pipeline(readFileStream, csvParser, writeFileStream); } readByLine(path, opt);

cuando ejecuto el archivo, obtengo:

 TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be of type string or an instance of Buffer or Uint8Array. Received an instance of Object at new NodeError (node:internal/errors:387:5) at _write (node:internal/streams/writable:315:13) at Writable.write (node:internal/streams/writable:337:10) at Parser.ondata (node:internal/streams/readable:766:22) at Parser.emit (node:events:513:28) at Readable.read (node:internal/streams/readable:539:10) at Parser.<anonymous> (/home/gmail-username/node/maricopa/node_modules/csv-parse/dist/cjs/index.cjs:1357:28) at Parser.emit (node:events:513:28) at emitReadable_ (node:internal/streams/readable:590:12) at process.processTicksAndRejections (node:internal/process/task_queues:81:21) { code: 'ERR_INVALID_ARG_TYPE' }

¿Cómo puedo arreglar esto?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Las lecturas del flujo csvParser están en modo objeto .

Un fs.writeStream requiere que se escriban cadenas/búferes.

Se puede implementar un flujo de transformación simple para convertir los objetos en una cadena JSON configurando la opción writableObjectMode :

 class JsonTransform extends Transform { constructor(opt) { super(Object.assign({}, { writableObjectMode: true }, opt)); } _transform(obj, enc, callback) { let ret = JSON.stringify(obj) + '\n' this.push(ret) callback() } }

Luego use eso entre parse + file write.

 const toJSON = new JsonTransform() await pipeline(readFileStream, csvParser, toJSON, writeFileStream);

Creo que el proyecto stringify csv es similar para escribir CSV.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!