88020

req.body is undefined - nodejs

Here is my html:

<form action='/new' method='POST' > <span>pool name: </span> <input type="text" name="name" /> <input type="hidden" name="imageSrcList" /> <button type='submit' >Create new</button> </form>

And here is the relevant JS:

var app = express() app.use(fileUpload()); app.set('view engine', 'ejs') app.use(express.static(__dirname + '/views')); <hr> app.post('/new', (req, res) => { console.log(req.body.name); })

The console reads out:

TypeError: Cannot read property 'name' of undefined

I have tried with console.log(req.body) and this also is undefined.

Thanks in advance!

Answer1:

You're missing the body-parser middleware which is necessary to have req.body set to a value. Express doesn't come with this by default and will need to be installed via NPM npm i --save body-parser

const bodyParser = require('body-parser') app.use(bodyParser.json()) app.use(bodyParser.urlencoded({extended: true}))

Recommend

  • How to use Defer in canjs
  • Passing a variable from a jade file to a React Component
  • Run socket.io 2.0 and Express 4.0+ on the same port
  • Why does internet explorer fail to download a PDF using NodeJS and Express?
  • How to achieve inheritance in ES6 with “webpack module bundler”?
  • Template in HTML, with Webpack, get error “variable” is not defined
  • Error with ffi module node.js Uncaught Error: Dynamic Linking Error: Win32 error 193
  • Correct way to handle 404 and 500 errors in express
  • Global session variable in express.js route?
  • NodeJS - Disable JSONP on specific routes
  • Express.js : POST data as KEY of a req.body object instead of VALUE of req.body?
  • opengl window freezing during move/resize
  • Can't connect Entity Framework to local SQL Server Express
  • Why must we declare a variable name when adding a method to a struct in Golang?
  • Python ImageIO Gif Set Delay Between Frames
  • How do I display a dialog that asks the user multi-choice questıon using tkInter?
  • Button click event not firing in jQuery
  • Textfile Structure (tables)
  • chrome.tabs.executeScript only fires when the Developer Console is open
  • JSON response opens as a file, but I can't access it with JavaScript
  • Spring Data JPA custom method causing PropertyReferenceException
  • Is calc() supported in html email?
  • Uncaught Error: Could not find module `ember-load-initializers`
  • Modifying destination and filename of gulp-svg-sprite
  • sending/ receiving email in Java
  • Symfony2: How to get request parameter
  • Importing jscolor library in angular 2
  • Akka Routing: Reply's send to router ends up as dead letters
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • Is there a mandatory requirement to switch app.yaml?
  • Comma separated Values
  • How to delete a row from a dynamic generate table using jquery?
  • Proper way to use connect-multiparty with express.js?
  • Error creating VM instance in Google Compute Engine
  • using HTMLImports.whenReady not working in chrome
  • Hits per day in Google Big Query
  • how does django model after text[] in postgresql [duplicate]
  • Authorize attributes not working in MVC 4
  • EntityFramework adding new object to nested object collection
  • Checking variable from a different class in C#