0

I need to do these processes in order but I don't know how to go about doing this in nodeJS. I know this is a simple question but I am new to nodeJS so any help would be appreciated.

var pictureID=0;
        var name2=0;

            getData();

            function getData() {
                req.pipe(req.busboy);
                getFile();


                function getFile() {
                    req.busboy.on('file', function (fieldname, file, filename, encoding, mimetype) {
                        console.log('File [' + fieldname + ']: filename: ' + filename);
                        var gfsstream = gfs.createWriteStream('pic');
                        file.pipe(gfsstream).on('finish', function () {
                            console.log('File [' + fieldname + '] Finished');
                            console.log(gfsstream.id);
                            pictureID = gfsstream.id;
                            console.log('picture ID is:' + pictureID);
                        });
                    });
                    getField();
                }

                function getField() {
                    req.busboy.on('field', function (fieldname, val, fieldnameTruncated, valTruncated) {
                        console.log('Field [' + fieldname + ']: value: ' + inspect(val));
                        var name2 = 'david';
                    });
                    finished();
                }

                function finished() {


                req.busboy.on('finish', function () {
                    console.log('Done parsing form!');
                    console.log('Picture id is:' + pictureID + ' name' + name2);
                });
                saveData();
            }

            }
onemillion
  • 682
  • 5
  • 19
  • Hopefully the answer to [this question](http://stackoverflow.com/questions/19739755/nodejs-callbacks-simple-example) regarding callbacks in node.js might help. – Mark Aug 24 '15 at 09:58

1 Answers1

0

Here is what you might be looking for, Let me know if it works as expected

var pictureID=0;
var name2=0;

getData();

function getData() {
    req.pipe(req.busboy);
    getFile(getField);

}
var getFile=function(callback) {
    req.busboy.on('file', function (fieldname, file, filename, encoding, mimetype) {
        console.log('File [' + fieldname + ']: filename: ' + filename);
        var gfsstream = gfs.createWriteStream('pic');
        file.pipe(gfsstream).on('finish', function () {
            console.log('File [' + fieldname + '] Finished');
            console.log(gfsstream.id);
            pictureID = gfsstream.id;
            console.log('picture ID is:' + pictureID);
            callback(finished);
            //getField();
        });

    });

}
var getField=function(callback) {
    req.busboy.on('field', function (fieldname, val, fieldnameTruncated, valTruncated) {
        console.log('Field [' + fieldname + ']: value: ' + inspect(val));
        var name2 = 'david';
        callback();
        //finished();
    });

}
var finished=function (callback) {
    req.busboy.on('finish', function () {
        console.log('Done parsing form!');
        console.log('Picture id is:' + pictureID + ' name' + name2);
        callback(saveData);
        //saveData();
    });

}
Manoj Gupta
  • 578
  • 5
  • 10