0

I'm writting an application which receives forms and I have a little problem. My application freezes when no files is uploading. I have no idea why. I use connect-busboy to receive files.

That's my code:

req.busboy.on('file', function(fieldname, file, filename) {
            switch(fieldname){
                case 'mainPhoto':
                    if(filename != '' && filename != null && filename != "null") {
                        console.log("w mainPhoto w if'ie")
                        var n = filename.lastIndexOf(".");
                        var end = filename.substring(n+1, filename.length);
                        mainPhoto = "mh_" + id + "." + end;
                        fstream = fs.createWriteStream(__dirname + '/static/photos/' + mainPhoto);
                        file.pipe(fstream);
                        fstream.on('close', function() {
                            console.log("End of saving hospital's main photo.");
                        });
                        break;
                    }
                    else {
                        mainPhoto = null
                        break;
                    }

                case 'photos':
                    if(filename != '' && filename != null && filename != "null") {
                        var n = filename.lastIndexOf(".");
                        var end = filename.substring(n+1, filename.length);
                        var photohoto;
                        photo = "h_" + id + "_" + photosCount + "." + end;
                        photos.push(photo);
                        fstream = fs.createWriteStream(__dirname + '/static/photos/' + photo);
                        file.pipe(fstream);
                        photosCount++;
                        fstream.on('close', function() {
                            console.log("End of saving hospital's main photo.");
                        });
                        break;
                    }
                    else {
                        break;
                    }


                case 'priceList':
                    if(filename != '' && filename != null && filename != "null") {
                        var n = filename.lastIndexOf(".");
                        var end = filename.substring(n+1, filename.length);
                        priceList = "pricelist_" + id + "." + end;
                        fstream = fs.createWriteStream(__dirname + '/static/price_lists/' + priceList);
                        file.pipe(fstream);
                        fstream.on('close', function() {
                            console.log("End of saving hospital's price list.");
                        });
                        break;
                    }
                    else {
                        priceList = null;
                        break;
                    }
            }
        });

I have to add all files to avoid it, but that's not the thing I wanted to do.

Any help is apprecited. Thanks!

kuba12
  • 255
  • 1
  • 4
  • 15
  • At some point you need to send back a response to the client. You left out the rest of the upload handler so difficult to say if that's causing your problems. – robertklep Nov 14 '15 at 19:45
  • That's my whole function: http://pastebin.com/Gnq6Yu8d Code from "finish" doesn't execute at all. – kuba12 Nov 14 '15 at 20:12
  • TBH, I think you should refactor your code, it's way too nested and complicated. It may be that the `finish` event is called before your database connection/query has completed, for instance. Also, creating a new database connection for each request is not very performant and almost always unnecessary (just open it once and reuse it during the lifetime of your application). – robertklep Nov 14 '15 at 20:51
  • Ok, but the point is that everything works fine while uploading all files and when for example I upload only two of them application freezes. – kuba12 Nov 14 '15 at 21:10
  • That's most likely caused by the `finish` event to get called too fast (because `busboy` has nothing to do), before you even set a handler for it. In other words, it probably fires at a point in time when you're not yet listener for it (and events aren't "queued" or anything: if there isn't a listener for it, the event gets "lost"). – robertklep Nov 15 '15 at 10:16

0 Answers0