V
V
Vitaly2016-07-17 13:22:36
JavaScript
Vitaly, 2016-07-17 13:22:36

Node.js fs. How it is possible to modify the data in a stream?

Good day everyone, the essence of the problem is as follows, I parsed the bigdata and pushed the data to the file, I forgot that json objects should be separated by a comma, I wrote these crutches:

let fs = require('fs');
let writer = fs.createWriteStream('data_valid.json', {flags: 'w'});
let reader = fs.createReadStream('data.json',
    {
        flags: 'r',
        encoding: 'utf8'
    });
writer.on('finish', function () {
    console.error('Запись выполнена успешно.');
});


reader.on('readable', function() {
    let buf;
    while ((buf = reader.read()) !== null) {

        writer.write(buf+',');

    }
});

All is well, all objects get into buf one by one, but buf+',' doesn't happen.
What am I doing wrong?
I would be grateful for clarifications.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir, 2016-07-17
@Vovchikvoin

And I want to ask what is the problem with just copying the file, you are not doing anything else.

D
Dmitry Belyaev, 2016-07-18
@bingo347

Вы не завершаете поток записи и даже не флушаете его на диск, все данные остаются в оперативке и дохнут вместе с процессом
reader.on('end', () => writer.end());

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question