Hulk Bulk Hulk Bulk - 5 months ago 55
Javascript Question

Undefined while sending post nodejs

I try to send a form with data in my nodejs app but I get "undefined" I am not sure does it matter but in console first I see "undefined", then POST. I have tried some "fixes" from the Internet like adding "type:'application/*+json', inflate: false" to app.use(bodyParser.json...) but nothing worked for me...

Undefined
POST /users/register 200 32.777 ms - 2346


this is my routes/users.js file:

var express = require('express');
var router = express.Router();

router.get('/register', function(req, res, next) {
res.render('register', {
'title': 'Register'
});
});

router.post('/register', function(req, res, next) {
console.log(req.body.name);
});

module.exports = router;


heres my jade file

form(method='post', action='/users/register', role='form')
.form-group
label Name
input.form-control(type='text', name='name', placeholder='Enter Name')
input.btn.btn-default(name='submit', type='submit', value='Register')


and app.js

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var expressValidator = require('express-validator');
var cookieParser = require('cookie-parser');
var session = require('express-session');
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var bodyParser = require('body-parser');
var flash = require('connect-flash');
var mongo = require('mongodb');
var mongoose = require('mongoose');
var db = mongoose.connection;

var routes = require('./routes/index');
var users = require('./routes/users');

var app = express();

app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use(logger('dev'));
app.use(bodyParser.json({ type: 'application/*+json' }));

app.use(session({
secret: 'secret',
saveUninitialized: true,
resave: true
}));

app.use(passport.initialize());
app.use(passport.session());

app.use(expressValidator({
errorFormatter: function(param, msg, value) {
var namespace = param.split('.'),
root = namespace.shift(),
formParam = root;
while(namespace.length) {
formParam += '[' + namespace.shift() + ']';
}
return {
param: formParam,
msg: msg,
value: value
};
}
}));

app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use(flash());
app.use(function(req, res, next) {
res.locals.messages = require('express-messages')(req, res);
next();
});

app.use('/', routes);
app.use('/users', users);

app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});

if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}

app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});


module.exports = app;


Routing works fine what show setting up a title to "Register", but I have problems with sending post :/

Answer

You're configuring body-parser to only accept JSON request bodies, but your form is submitted in URL-encoded format (application/x-www-form-urlencoded).

For that, you need to add the correct parser:

app.use(bodyParser.urlencoded({ extended : true }));

More information, also on the meaning of extended : true, here.

Comments