Hints needed: User registration form with photo upload using Multer

54 views
Skip to first unread message

sin...@gmail.com

unread,
Mar 26, 2015, 3:04:22 PM3/26/15
to nod...@googlegroups.com

Hi folks
Thanks for helping me out with my previous question about asynchronous behaviour and callback!

Now I have a bit related issue, where I am stuck.

I have a user registration form wich works nicely. Now I am trying to change that form to support user photo upload as well.
I am using Multer module and in the beginning everything worked fine.

The problem comes when the user is not selecting any photo to be uploaded.

I am saving the photos in a folder and storing to the DB (MySQL) all the filenames. The problem is that I cant get the function addUserToDB(req, res) called from a context where the file name is known. If the photo file is given, I could call addUserToDB inside the callback "onFileUploadComplete", however this never works if the photo file is not given because there is not going to happen any file upload. On the other hand, I could place this call inside "onParseEnd", which is always executed, BUT I have no more access to the file name! I can't get the photo name from res.body.filename anymore, its a different context :-( 

Any hints how would you implement this would be a great help. Thanks in advance.

var multer1 = multer({ dest: '../uploads/',
     rename: function (fieldname, filename) {
         return filename+Date.now();
     },
     onFileUploadStart: function (file) {  },

     onFileUploadComplete: function (file, req, res) {
        done=true;
     },

     onParseEnd : function (req, next) {
        // next();
     }
     });

router.post('/registerwithphoto', multer1, function(req, res) {
     addUserToDB(req, res)
})

Reply all
Reply to author
Forward
0 new messages