Through : how to output things

47 views
Skip to first unread message

Roelof Wobben

unread,
Jan 19, 2015, 1:25:41 PM1/19/15
to nod...@googlegroups.com
Hello,

I try to solve a nodeschool exercise where a json file is the input.

So far I have this :

~~~

var combine = require('stream-combiner');
var through = require('through');
module.exports = function() {
   
    var group = through (write,end)
   
    function write(line) {
       
        if (line.length === 0) { return  } ;
        row = JSON.parse(line);
        this.queue(row);
      
    }
       
     function end(row) {
         console.log(row)
         this.queue(null);
     }
   
    return grouper

~~~

but if I do node file.js { 'test' : test } I see no output.

Can someone help me how to output the row variable.
I tried console.log(row) but that did not do the job.

Roelof

greelgorke

unread,
Jan 19, 2015, 5:28:09 PM1/19/15
to nod...@googlegroups.com
your module returns undefined because there is no var named grouper. but a group there is

Ryan Schmidt

unread,
Jan 19, 2015, 10:50:05 PM1/19/15
to nod...@googlegroups.com

On Jan 19, 2015, at 12:25 PM, Roelof Wobben wrote:
>
> I try to solve a nodeschool exercise where a json file is the input.
>
> So far I have this :
>
> ~~~
>
> var combine = require('stream-combiner');
> var through = require('through');
> module.exports = function() {
>
> var group = through (write,end)
>
> function write(line) {
>
> if (line.length === 0) { return } ;
> row = JSON.parse(line);
> this.queue(row);
>
> }
>
> function end(row) {
> console.log(row)
> this.queue(null);
> }
>
> return grouper
>
> ~~~

If this is the entire file, it is a syntax error because you are missing a closing curly bracket at the end of the file.

You also have exported a function... but where do you invoke that function?


> but if I do node file.js { 'test' : test } I see no output.

Are you trying to supply "{ 'test' : test }" to the script's stdin perhaps? If so, the way to do that on UNIX is:

echo "{ 'test' : test }" | node file.js


Roelof Wobben

unread,
Jan 20, 2015, 2:40:12 AM1/20/15
to nod...@googlegroups.com


Op dinsdag 20 januari 2015 04:50:05 UTC+1 schreef ryandesign:

On Jan 19, 2015, at 12:25 PM, Roelof Wobben wrote:
>
> I try to solve a nodeschool exercise where a json file is the input.
>
> So far I have this :
>
> ~~~
>
> var combine = require('stream-combiner');
> var through = require('through');
> module.exports = function() {
>    
>     var group = through (write,end)
>    
>     function write(line) {
>        
>         if (line.length === 0) { return  } ;
>         row = JSON.parse(line);
>         this.queue(row);
>        
>     }
>        
>      function end(row) {
>          console.log(row)
>          this.queue(null);
>      }
>    
>     return grouper
>
> ~~~

If this is the entire file, it is a syntax error because you are missing a closing curly bracket at the end of the file.

You also have exported a function... but where do you invoke that function?



and how do I invoke this function. That is something I did not learn at nodeschool.io at this moment

Roelof
 

Ryan Schmidt

unread,
Jan 20, 2015, 8:29:51 PM1/20/15
to nod...@googlegroups.com
Is the code you showed above the entire code of your project?

What are you trying to do?

When you assign something to module.exports, that implies you're trying to create a module. For example, if you have a file hello.js which contains:

module.exports = function (cb) {
cb(null, "Hello!");
}

Then you can use that module in another file, let's say app.js, by writing:

var hello = require('./hello.js');
hello(function (err, result) {
if (err) return console.error(err);
console.log(result);
});

When you run "node app.js" this will output:

Hello!

You can read much more about how modules work by reading:

http://nodejs.org/api/modules.html


Reply all
Reply to author
Forward
0 new messages