Hulk Bulk Hulk Bulk - 1 year ago 133
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...

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'
});'/register', function(req, res, next) {

module.exports = router;

heres my jade file

form(method='post', action='/users/register', role='form')
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(bodyParser.json({ type: 'application/*+json' }));

secret: 'secret',
saveUninitialized: true,
resave: true


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(express.static(path.join(__dirname, 'public')));

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

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

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

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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download