Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There is no need to write the data to a file, then process the file; nor do you need to buffer the whole data before processing it.</p> <p>If the data you're outputting is in JSON, I'd suggest then using <a href="https://github.com/dominictarr/JSONStream" rel="nofollow">JSONStream</a> in the parent code. This will allow you to parse the output on the fly. Below is an example.</p> <p>The child code will output a JSON array:</p> <pre><code>// Child code console.log('['); // We'll output a JSON array for (var j = 0, lUF = uniqueFlyers.length; j &lt; lUF; j++) { var products = uniqueFlyers[j].products; for (var k = 0, lP = products.length; k &lt; lP; k++) { var pstr = products[k].product; console.log(JSON.stringify(pstr)); // output some JSON if ((j !== lUF - 1) &amp;&amp; (k !== lP - 1)) console.log(','); // output commas between JSON objects in the array cntSent+=1; } } console.log(']'); // close the array </code></pre> <p>While the parent code will read this JSON array, and process it. We use the <code>*</code> selector in order to select all elements of the array. The JSONStream will then emit each JSON document one by one, as they are parsed. Once we have this data, we can then use a <a href="http://nodejs.org/api/stream.html#stream_class_stream_writable" rel="nofollow">Writable stream</a>, that will read the JSON objects and then do something (anything!) with them.</p> <pre><code>// Parent code var stream = require('stream'); var jsonstream = require('JSONStream').parse('*'); var finalstream = new stream.Writable({ objectMode: true }); // this stream receives objects, not raw buffers or strings finalstream._write = function (doc, encoding, done) { console.log(doc); done(); }; proc.stdout.pipe(jsonstream).pipe(finalstream); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload