1

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?

Timothy Truckle
  • 15,071
  • 2
  • 27
  • 51
Saikiran
  • 756
  • 2
  • 11
  • 29
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Brahma Dev Sep 21 '17 at 10:38
  • 1
    Because you're calling `callback` before the file is downloaded. – Brahma Dev Sep 21 '17 at 10:38
  • I didn't get you. The callback is a part of async series function and I'm trying to replicate it. Do you mean I should remove callback in the first function for it to operate properly? – Saikiran Sep 21 '17 at 10:58
  • `callback(null);` does not wait for `form.parse` to finish, same for `fs.appendFile`. It should be called after `res.end` and `console.log('Saved!');` respectively. – Brahma Dev Sep 21 '17 at 11:01
  • I changed the format. Node now crashes immediately after downloading the file and it doesn't run the second function at all. – Saikiran Sep 21 '17 at 11:13
  • Mind sharing the error details? – Brahma Dev Sep 21 '17 at 11:18
  • This page isn’t working localhost didn’t send any data. ERR_EMPTY_RESPONSE – Saikiran Sep 21 '17 at 11:25
  • This is the immediate response in the browser – Saikiran Sep 21 '17 at 11:26
  • I am looking for the error in the program which is probably running in a `terminal`/`command prompt` window and not the browser. – Brahma Dev Sep 21 '17 at 11:28
  • There's no error in the command line. Pretty weird. It shows the logs of req.body that i logged out. – Saikiran Sep 21 '17 at 11:32
  • [nodemon] starting `node app.js` Node.js listening on port 8080 "ITPL" { deviceinput: 'ITPL' } "undefined" {} [nodemon] restarting due to changes... [nodemon] starting `node app.js` Node.js listening on port 8080 – Saikiran Sep 21 '17 at 11:32
  • The app seems to restart as soon as the first functions ends – Saikiran Sep 21 '17 at 11:33

1 Answers1

2

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.

Brahma Dev
  • 1,955
  • 1
  • 12
  • 18