0

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
      });
}
dogonaroof
  • 69
  • 13
  • 1
    may be you have provided `action='/users/register'` and in router you have `/register`, do you have `users/` as a prefix before coming to this router.post ?? – Yogesh.Kathayat Aug 14 '18 at 06:45
  • no i dont have users/ as a prefix, if i add users/ ..it gives an error – Rahul Kumar Aug 14 '18 at 07:02
  • Ok, then change `action='/register'` in your jade code it will work – Yogesh.Kathayat Aug 14 '18 at 07:03
  • if i remove users from action it gives an error – Rahul Kumar Aug 14 '18 at 07:05
  • 1
    ok then may be you have `/users` already before coming to this route . have you used `app.use(express.bodyParser());` middle-ware in the file where you have initialized the app ? – Yogesh.Kathayat Aug 14 '18 at 07:09
  • nope, should I? – Rahul Kumar Aug 14 '18 at 07:12
  • yes, body-parser extract the entire body portion of an incoming request stream and exposes it on req.body. Install body-parser using NPM as . `npm install body-parser --save` and then include it `app.use(express.bodyParser());` – Yogesh.Kathayat Aug 14 '18 at 07:14
  • its giving an error "Error: Most middleware (like bodyParser) is no longer bundled with Express and must be installed separately. Please see https://github.com/senchalabs/connect#middleware." – Rahul Kumar Aug 14 '18 at 07:19
  • ok for newer version of express you need to use it like below :- `app.use(bodyParser.urlencoded({ extended: true })); app.use(bodyParser.json());` – Yogesh.Kathayat Aug 14 '18 at 07:20
  • nope, it doesnt help ..can you share your apple id so i can sare my screen with you – Rahul Kumar Aug 14 '18 at 07:27
  • Try adding this `app.use(express.urlencoded({ extended: true }));` and `app.use(express.json());`. Not `bodyParser` – onoya Aug 14 '18 at 09:13
  • nope, it doesn't help – Rahul Kumar Aug 14 '18 at 10:56
  • It looks like it has something to do with `multipart/form-data`. `body-parser` does not handle multipart bodies. Try using a middleware like [multer](https://github.com/expressjs/multer). There are several other modules suggested in their [documentation](https://www.npmjs.com/package/body-parser#readme) for handling multipart bodies. – onoya Aug 15 '18 at 02:51

1 Answers1

0

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.

onoya
  • 2,268
  • 2
  • 17
  • 17