I want to write a full JSON object to a file as it comes in. I'm currently using
const file = fs.createWriteStream('./example.file');
var inputStream = JSON.parse(generatedData);
fs.write(inputStream+"\n");
The stream I'm trying to capture comes from a Leap Motion controller frame.dump . I've attempted to write the object using both JSON.parse() and JSON.stringify() , but it only seems to write undefined lines.
I'm probably not handling the object correctly, but I don't know how to resolve it.
EDIT: For clarification, I know how to write single elements of the JSON object, but I would prefer to dump the entire JSON to each new line.
generatedData
is already a JSON string here and you are now parsing it back to an object which is not needed.
As you want to append to the file, I would consider appendFile
fs.open('./example.txt', 'a', (err, fd) => {
if (err) throw err;
fs.appendFile(fd, generatedData, 'utf8', (err) => {
fs.close(fd, (err) => {
if (err) throw err;
});
if (err) throw err;
});
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.