adelriosantiago
adelriosantiago

Reputation: 8134

Why node spawn() returns "undefined" at the beginning

Using Node's spawn() function always returns "undefined" at the beginning, like in the following real example:

undefined M text.txt M text_2.txt

This is the command git status --porcelain on a testing repository, this same command executed on the command line interface shows (command included):

$ git status --porcelain

M text.txt

M text_2.txt

The spawn() events are written like this:

  spawn.stdout.setEncoding('utf8');
  spawn.stderr.setEncoding('utf8');

  spawn.stdout.on('data', function(d){ 
    data += d;
  });
  spawn.stderr.on('data', function(e){
    err += e;  
  });

  spawn.on('exit', function(){
    callback(err, data);
  });

Why is spawn() returning "undefined" at the beginning and how do I solve it?

Upvotes: 0

Views: 454

Answers (1)

SLaks
SLaks

Reputation: 888185

You never declared or initialized data.
Therefore, it starts as undefined, and you're concatenating to that.

Upvotes: 2

Related Questions