Sunday, 19 March 2017

Cannot read property 'transfer-encoding' of undefined - multiparty / multer

I am getting above error when trying to upload an image using multer with multiparty. Below code worked fine without the multiparty with only one field ie. only file field. But I want to add more details including name, email and the image field.

      var storage = multer.diskStorage({
                            destination: function(req, file, cb){
                                cb(null, './uploads'); 
                                       console.log(file); 
                            },
                            filename: function(req, file, cb){
                                cb(null, file.image[0].fieldName + '-' + Date.now() + '.jpg');                
                            }
                        });

      var upload = multer({storage:storage}).single('image');

      create: function(req,res){   

              var form = new multiparty.Form();

              form.parse(req, function(err, fields, file) {
              req.body = fields;

              let name = req.body.name;
              let email = req.body.email;

             upload(file, res, function(err){
              if(err) {
                console.log(err)
              }
              else {
                console.log('no error');
              }
            })
        }

Thanks.



via ikhan

No comments:

Post a Comment