11

I am using a middleware body-parser to encoded the form values to get in req.body object. But as I debug my code, found out req.body is undefined. Here is my code

var express = require('express');
var app = express();
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: false }));

Listen Post request

app.post('/newCategory', function (req,res) {

            //express attached the form encoded values into body
            var categoryName = req.body.categoryName;
        });

Html Form

<form action="/newCategory" role="form" method="post" class="form-inline">
    <input type="text" name="categoryName" placeholder="Category name" class="form-control" />
    <input type="submit" value="New Category" class="btn btn-primary" />
</form>
Ammar Khan
  • 2,565
  • 6
  • 35
  • 60
  • possible duplicate of [Express.js req.body undefined](http://stackoverflow.com/questions/9177049/express-js-req-body-undefined) – dmigo Jul 25 '15 at 12:26

6 Answers6

12

Just ran into the same issue. It looks like I resolved my problem by moving my code to map routes after the urlencoded line. I am now seeing req.body in the post.

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


// Map routes
var controllers = require("./controllers");
controllers.init(app);
user3926346
  • 121
  • 2
  • Here is somewhat similar answer in a somewhat similar discussion http://stackoverflow.com/a/13779626/788833 – dmigo Jul 25 '15 at 12:25
7

This solved the problem for me

var bodyParser = require('body-parser');
var app=express();
app.use(bodyParser.urlencoded());
app.use(bodyParser.json());

Hope this help

ackuser
  • 5,681
  • 5
  • 40
  • 48
4

I noticed that the order is very important. Usually the router should be declared in the end before starting the server . Eg: 1.i import the required files

var express            = require("express");
var bodyParser         = require("body-parser");
var morgan             = require("morgan");
var db              = require("./db.js");

var app                = express();

2.i declare the other stuffs

app.set("port", process.env.PORT || 3000);

//app.use(express.static(__dirname + '/public'));
app.use(morgan('dev') ); // Log every request to console
app.use(bodyParser.urlencoded({
    extended: true
}));

app.use(bodyParser.json());

3. AFTER I INCLUDE ROUTES - most important step

var routes = require('./routes/routes');
routes(app);   //routes shall use Express
  1. start the server

    app.listen(app.get("port"), function () { console.log("Express server listening on port " + app.get("port")); });

And then will work .. i'm not sure why but after it happen few time i learn the lesson .

Teodor
  • 1,285
  • 4
  • 21
  • 37
2

If you're using urlencoded with { extended:false }, req.body will return the unparsed raw string from the form categoryName=test. Meaning req.body.categoryName will be undefined.

Try passing true so it can parse the form data using the qs module.

app.use(bodyParser.urlencoded({
    extended: true
}));
Ben Fortune
  • 31,623
  • 10
  • 79
  • 80
1

As body-parser module is used to parse the body and urls, it should be called before any call to "req.body..."

var bodyParser = require("body-parser");
///////req.body is undefined here
//extended: false means you are parsing strings only (not parsing images/videos..etc)
app.use(bodyParser.urlencoded({extended: false});
///////you req.body is working here (module below is using req.body)
app.use("/", module);
app.post('/newCategory', function (req,res) {
     var categoryName = req.body.categoryName;
});
Muhammad Soliman
  • 21,644
  • 6
  • 109
  • 75
0

I am use this:

const app = express(); app.use(bodyParser.urlencoded({ extended: true })); app.use(express.json()); but verify if your POSTMAN, for he can be the problem