does not write to the created stream writable node.js

342 Views Asked by At
let testWriteableStream_1 = fs.createWriteStream("logs/test_profit_1.csv", { flags: 'a' });
let testWriteableStream_2 = false;
let testFlag = { number: 1 };
let testCount = { number: 0 };
let testCountAll = { number: 0 };

function TestWritable2(testWriteableStream_1, testWriteableStream_2, testFlag, testCount, testCountAll) {
  let time = new Date().getTime();
  console.log('time:', time);
  if (testCount.number === 5) {
    testWriteableStream_2 = fs.createWriteStream(`logs/test2_profit${time}.csv`, { flags: 'a' });
    console.log('testWriteableStream_2._writableState 5:----------------------------------------------', testWriteableStream_2._writableState);
  }
  if (testCount.number === 10) {
    console.log('testWriteableStream_2._writableState 10:----------------------------------------------', testWriteableStream_2._writableState);
    testWriteableStream_2.write(`writeableStream_${testCountAll.number}\r\n`);
  }
  testCount.number++;
}


 TestWritable2(testWriteableStream_1, testWriteableStream_2, testFlag, testCount, testCountAll);

The function is called in websocket ‘message’ event. Message error: iteableStream_2.write is not a function

testWriteableStream_2.write(`writeableStream_${testCountAll.number}\r\n`);
                      ^
TypeError: testWriteableStream_2.write is not a function
1

There are 1 best solutions below

6
Mureinik On

In the case of testCount.number === 10 you don't create a stream, and testWriteableStream_2 remains false, which doesn't have a write method. Create a stream for this case too, and you should be OK.