Reputation: 53
i need to store a stream object as a string,
i tried to use JSON.stringify(stream)
but i got the following error:
TypeError: Converting circular structure to JSON
so i have tried to use "circular-json" package like this:
var o = CircularJSON.parse(CircularJSON.stringify(stream))
but when accessing the stream write function i get an error -
o.write is not a function
how can i store stream objects as string, and parse them later to the original object?
Upvotes: 4
Views: 7908
Reputation: 20355
handling streams can be complicated. for stream -> string
, i recommend using this library: https://www.npmjs.com/package/raw-body
Upvotes: 0
Reputation: 191829
Streams don't contain the raw data as part of the object. Instead they emit the data as part of 'data'
events that you can listen to. Subscribe to the .on
event for the stream and when it emits data you can append it to a string and get all the data.
On the 'end'
event you have finished reading all the data so you can do something with the data in the 'end'
event callback.
let data = '';
stream.on('data', chunk => data += chunk);
stream.on('end', doSomethingWithData);
Upvotes: 4