简体   繁体   English

高地消耗和toArray方法一起

[英]Highland consume and toArray method together

Hi people and happy holidays! 大家好,节日快乐!

I'm trying to consume a stream of csv rows with highland. 我试图消耗高地的csv行流。 To do some special processing and avoid passing down the headers to the stream, I'm calling .consume() and then I wanted the outcome in an array. 为了进行一些特殊处理并避免将标头传递到流中,我调用了.consume() ,然后我希望将结果放在数组中。 The problem is that the callback of .toArray() is never called. 问题在于,从未调用.toArray()的回调。 I'm just curious about this as I already changed my solution to .map().toArray() but I still think .consume() would be a more elegant solution. 我只是对此感到好奇,因为我已经将解决​​方案更改为.map().toArray()但我仍然认为.consume()将是一个更优雅的解决方案。 ;) This is the piece of code that reads from a csv file and processes the rows: ;)这是一段从csv文件读取并处理行的代码:

const fs = require('fs');
const _ = require('highland');

const dataStream = fs.createReadStream('file.csv', 'utf8');

_(dataStream)
  .split()
  .consume((err, row, push, next) => {
    console.log('consuming a row', row); // <-- this shows data

    if (err) {
      push(err);
      return next();
    }

    // This condition is needed as .consume() passes an extra {} at the end of the stream <-- donno what this happens!!
    if (typeof row !== 'string') {
      return next();
    }

    const cells = row.split(',');

    if (cells[0] === 'CODE') { // <-- header row
      const { columnNames, columnIndexes } = processHeader(cells)); // <-- not relevant, works okay
      return next();
    }

    console.log('processin content here', processRow(cells)); // <-- not relevant, works okay
    push(null, processRow(cells));
    return next();
  })
  .toArray(rows => console.log('ROWS: ', rows)); // <-- I don't get anything here

Thanks a lot! 非常感谢!

Both consume and toArray consume a stream. consumetoArray消耗一个流。 You can only consume a stream once. 您只能使用一次流。 If you try to consume it multiple times the "stream" will be empty. 如果您尝试多次使用它,则“流”将为空。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM