1
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
Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
Illusion
  • 71
  • 9

1 Answers1

0

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.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • Yes. in the case of testCount.number === 10 I do not create it, because I have already created it at this iteration testCount.number === 5. And the stream is declared in the global scope and is passed to the function. I believe that I pass testWriteableStream_2 = false to the function, then at the 5th iteration I create a stream, then at the 10th iteration I write to this stream, but for some reason it is no longer there. – Illusion Jul 03 '21 at 14:23
  • @Illusion testWriteableStream_2 is an argument name - you are changing a local variable in the function's scope – Mureinik Jul 03 '21 at 14:24
  • Why is that? I declared the variable testWriteableStream_2 outside and passed it to the function. Inside the function, I did not declare this variable, so it must take it from the function's arguments or from the outer scope, where it was declared. – Illusion Jul 03 '21 at 16:14
  • @Illusion The function argument acts as a local variable. When inside the function, it **hdies** the variable with the same name from outside the function – Mureinik Jul 03 '21 at 16:15
  • Got it. I passed true, and boolean values ​​are passed by value, not send. You need to pass the object or as a property of the object, as I did with counters. – Illusion Jul 03 '21 at 16:19
  • This means that in order to achieve my goal, I should not overwrite the entire reference variable, an object with a property. For example: ``` let testWriteableStream_1 = { write_1: fs.createWriteStream ("logs / test_profit_1.csv", {flags: 'a'}) }``` and change this property of the object inside the function – Illusion Jul 03 '21 at 16:42