javascript - How to write big array to .txt file using node.js? -


the obvious solution fs.writefile, assume.

but answer of this question suggests should using stream technique.

i'm trying code in order remove line text file converting array:

var index = randomintfrominterval(1, unfinished_searches.length-1); // remove index unfinished_searches.splice(index, 1);  fs.truncate('unfinished_searches.txt', 0, function() {     var file = fs.createwritestream('unfinished_searches.txt');     file.on('error', function(err) { /* error handling */ });     unfinished_searches.foreach(function(v) { file.write(v.join(', ') + '\n'); });     file.end(); }) 

which returns following error:

typeerror: undefined not function 

at join in line:

unfinished_searches.foreach(function(v) { file.write(v.join(', ') + '\n'); }); 

if want remove line big text file, consider use pipes. create read stream, pipe "checking function", , pipe write stream.

this way don't have load whole file memory. in memory small amount of data @ once. of course can't write same file. after complete operation, can rename file , delete old one.

edit: post can usefull you: parsing huge logfiles in node.js - read in line-by-line


Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -