how to add user's information in mongoose database?

185 views Asked by At

i have a registration form in .jade format

form(method='post', action='/users/register', enctype='multipart/form-data')
      .form-group
          label Name
          input.form-control(name='name', type='text', placeholder='Enter Name' required)
      .form-group
          label Email
          input.form-control(name='email', type='email', placeholder='Enter Email' required)
      .form-group
          label Username
          input.form-control(name='username', type='text', placeholder='Usernamee' required)
      .form-group
          label Password
          input.form-control(name='password', type='password', placeholder='Enter Password' required)
      .form-group
          label Password
          input.form-control(name='password2', type='password', placeholder='Confirm Password' required)
      .form-group
          label Profile Image
          input.form-control(name='profileimage', type='file')
      input.btn.btn-default(name='submit', type='submit', values='Register') 

and this is how i'm creating a new user and trying to add them to the database but the data is not being added into the database. The problem is it's not taking values from the form fields: name, email username, password are all NULL I don't understand why as it's supposed to take all values

router.post('/register', function(req, res, next) {
var newUser = new User({
          name: req.body.name, 
          email: req.body.email,
          username: req.body.username,
          password: req.body.password,
          profileImage: profileImageName
      });
}
1

There are 1 answers

0
onoya On

You need a body parsing middleware to parse incoming request bodies.

Express has a built-in middleware function(which is based on the body-parser module) to parse urlencoded bodies since v4.16.0 onwards. But this built-in parser does not handle multipart bodies.

Since you're trying to handle a multipart/form-data, you may need a middleware like multer.