Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

req.body not populating with form data

The following files are my attempt at submitting a POST request to my nodejs (express) server. The req.body does not populate any sort of data from my form. I have done many searches and found that many of the solutions out there for this particular problem include moving bodyparser before my routes and making sure to include a name in the form fields.
Any help is appreciated.

app.js

[variables]
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(validator({errorFormatter: -----}));
[other middleware]
require('./passport')(app, passport);
require('./routes')(app, passport);
[start server]

index.jade

block content
h1= title
p Please register below
.error-messages
    ul.errors
        if errors
            each error, i in errors
                li.alert.alert-danger #{error.msg}
form(method='post', action='/signup', enctype='multipart/form-data')
    .form-group
        label Name
        input.form-control(type='text', name='name', placeholder='Enter name')
    .form-group
        label Email
        input.form-control(type='email', name='email', placeholder='Enter email')
    .form-group
        label Username
        input.form-control(type='text', name='username', placeholder='Enter a username')
    .form-group
        label Password
        input.form-control(type='password', name='password')
    .form-group
        label Confirm Password
        input.form-control(type='password', name='passwordConfirm')
    input.btn.btn-primary.pull-right(type='submit', name='submit', value='Sign Up')

index.js (called from an app.post('/signup', ...)

var name = req.body.name,
    email = req.body.email,
    username = req.body.username,
    password = req.body.password,
    passwordConfirm = req.body.passwordConfirm;

name = 'meg';

console.log('name: ' + req.body.name);
console.log('email: ' + req.body.email);
console.log('name2: ' + name);
console.log(req.body);
console.log(req.headers);
console.log(req.is('json'));

console.log

name: undefined
email: undefined
name2: meg
{}
 { host: 'xx.xx.xx.xxx:xx',
 connection: 'keep-alive',
  'content-length': '623',
  'cache-control': 'max-age=0',
   accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
  origin: 'http://xx.xx.xx.xxx:xxxx',
  'user-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36      (KHTML, like Gecko) Chrome/42.0.2311.152 Safari/537.36 OPR/29.0.1795.60',
   'content-type': 'multipart/form-data; boundary=----     WebKitFormBoundaryYJyUQjMbfAn9Cr64',
  dnt: '1',
  referer: 'http://xx.xx.xx.xxx:xxxx/signup',
  'accept-encoding': 'gzip, deflate, lzma',
  'accept-language': 'en-US,en;q=0.8',
  cookie: 'connect.sid=s%3Au9o1F_i22gHj0ZCqpHQiKWDLBGOVU1MJ.6DdtQAqbR6sriI8zF3AjQy6y1TDMD93TMGI7fRIJ5mM' }
 false
like image 665
OieOie Avatar asked Dec 19 '22 03:12

OieOie


1 Answers

You're using the wrong enctype for the form, express by default does not support multipart ( which is used for file uploads ), you need to use x-www-form-urlencoded.

form(method='post', action='/signup', enctype='application/x-www-form-urlencoded')
like image 60
mfreitas Avatar answered Dec 21 '22 17:12

mfreitas