Search code examples
javascriptnode.jsexpressmean-stackbusboy

upload an image using busboy meanstack undefined


I'm trying to make a sign up form with an image upload ability so I take the values on the ejs side using this by a post and enctype = "multipart/form-data"

<form method="post" action= "/SignUp" enctype="multipart/form-data" >
    <div class="form-group">
        <label for="firstName">First name</label>
        <input type="text" name="firstName" id="firstName" class="form-control" value="<%= locals.firstName || '' %>" required />
    </div>
    <div class="form-group">
        <label for="lastName">Last name</label>
        <input type="text" name="lastName" id="lastName" class="form-control" value="<%= locals.lastName || '' %>" required />
    </div>
    <div class="form-group">
        <label for="username">Username</label>
        <input type="text" name="username" id="username" class="form-control" value="<%= locals.username || '' %>" required />
    </div>
    <div class="form-group">
        <label for="password">Password</label>
        <input type="password" name="password" id="password" class="form-control" required />
    </div>
    <div class = "from-group">
      <label for = "Image">Image</label>
      <input Content-Type = "multipart/form-data" type ="file" name = "Image" id = "Image" class = "form-control" required/>
    </div
    <br />
    <br />
    <div class="form-group">
        <button type="submit" class="btn btn-primary">Register</button>
        <a href="/login" class="btn btn-link">Cancel</a>
    </div>
</form>

and I handle it from the server side using busboy

  SignUp:function(req,res){
  let reg = new Registrations();
  var busboy = new Busboy({
    headers: req.headers,
    limits: {
      fileSize: 6*1024*1024 //2MB limit
    }
  });
    var stream;
    var fstream;
       busboy.on('field', function(fieldname, val, fieldnameTruncated, valTruncated) {
         if(fieldname == 'firstName')
         reg.firstName = val;
         else if (fieldname == 'lastName')
          reg.lastName = val;
          else if(fieldname == 'username')
          reg.username = val;
          else {
            reg.password = val;
          }


       })
      busboy.on('file', function(fieldname,file, filename,encoding,mimeType){
      stream = __dirname + '/img/' + filename;
      fstream = fs.createWriteStream(__dirname + '/img/' + filename);
      file.pipe(fstream);
      fstream.on('close', function(){
          reg.Image = stream;
        reg.save(function(err,reg){
          if(err){
            res.send(err.message)
            console.log(err);
          }else{
            console.log(reg);
          }
        })
      })
    })
    busboy.on('finish', function() {

    })
    res.render('login');

  }

it shows me this error every time I try it
TypeError: Cannot read property 'on' of undefined on the line

req.busboy.on('file', function(fieldname,file, filename,encoding,mimeType)

Can you please tell me what is this problem about ?


Solution

  • I used multer instead and it worked correctly

    https://github.com/expressjs/multer