Saikiran
Saikiran

Reputation: 776

Async series doesn't work in an order as expected

I have a function that downloads the user input(currently named app.json) from browser(client) to the server

function downloadUpload(callback){
    //Using formidable node package for downloading user input to server
    var form = new formidable.IncomingForm();
    form.on('fileBegin', function(name, file) {
        file.path = file.name;
    });

    form.parse(req, function(err, fields, files) {
        res.writeHead(200, { 'content-type': 'text/plain' });
        res.write('received upload:\n\n');
        res.end(util.inspect({ fields: fields, files: files }));
    });

    callback(null);


}

I have another function that takes the file downloaded above and converts it into required format(final.json) something like this.

 function UpdateCode(callback){

    var obj = fs.readFileSync('app.json', 'utf8');

    var object = JSON.parse(obj);
    var data2 = [];
    for (var j = 0; j < object.length; j++) {
        if (object[j].value == "TEST") {
            data2.push(object[j]);
        }
    }
    console.log(data2);
    fs.appendFile('final.json', JSON.stringify(data2), function(err) {
        if (err) throw err;
        console.log('Saved!');
    });

    callback(null);



}

I want them to run in an order, so I used async series method like this

async.series([
    downloadUpload, 
    UpdateCode


    ],function(err,result){
        if(err) throw err;
        else{
            console.log(result);
        }
    });

The problem is the file(app.json) is getting downloaded and an error is displayed saying that app.json doesn't exist in the current folder or directory. Where am I going wrong?

Upvotes: 1

Views: 411

Answers (1)

user2652134
user2652134

Reputation:

This is likely what you need.

function downloadUpload(callback) {
  //Using formidable node package for downloading user input to server
  var form = new formidable.IncomingForm();
  form.on('fileBegin', function(name, file) {
    file.path = "app.json";
  });

  form.parse(req, function(err, fields, files) {
    res.writeHead(200, {
      'content-type': 'text/plain'
    });
    res.write('received upload:\n\n');
    res.end(util.inspect({
      fields: fields,
      files: files
    }));
  });

  form.on('end', function() {
    callback(null);
  });
} 

function UpdateCode(callback) {

  var obj = fs.readFileSync('app.json', 'utf8');

  var object = JSON.parse(obj);
  var data2 = [];
  for (var j = 0; j < object.length; j++) {
    if (object[j].value == "TEST") {
      data2.push(object[j]);
    }
  }
  console.log(data2);
  fs.appendFile('final.json', JSON.stringify(data2), function(err) {
    if (err) throw err;
    console.log('Saved!');
    callback(null);
  });
}

async.series([
  downloadUpload,
  UpdateCode
], function(err, result) {
  if (err) throw err;
  else {
    console.log(result);
  }
});

Also use nodemon -e js app.js. Otherwise nodemon will restart the program as soon as the json uploads.

Upvotes: 2

Related Questions