嗨人和节日快乐!
我试图消耗高地的csv行。要进行一些特殊处理并避免将标题传递给流,我调用.consume()
然后我想要一个数组中的结果。问题是从不调用.toArray()
的回调。我对此感到好奇,因为我已经将解决方案更改为.map().toArray()
,但我仍然认为.consume()
将是一个更优雅的解决方案。 ;)这是从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
非常感谢!
答案 0 :(得分:0)
consume
和toArray
都使用了一个流。您只能使用一次流。如果您尝试多次使用它,“stream”将为空。