2

I'm using Jade with express and node.js to create a frontend for a database of flight logs as part of my training. I have confirmed my inputs have names, that my body parser is set to application/json, and that my routes appear after my middleware declarations, yet my req.body object remains undefined. My question is why. Here is one field in my jade form:

.row.col-sm-16
  div.input-group.form-control
    form(action='',method='POST')

      div(data-role='fieldcontain')
        fieldset(data-role='controlgroup')
          label(for='HobbsOut') HobbsOut &nbsp &nbsp &nbsp
             input(id='HobbsOut',type='text',value='',placeholder='hobbs in generated',name='HobbsOut')

Everything below the gap is repeated for each field. The submit button:

div.text-center
  button.btn.btn-default.input-group-button Submit

The button div is aligned with 'data-role' divs. The relevant route:

app.post('/logadd', ctrl.addFlight); 

And the relevant controller:

module.exports.addFlight = function (req, res) {
  console.log(req.body.HobbsOut);
  res.redirect('/loglist');
};

Currently I'm simply trying to print the first field and redirect the user to the flight log. I'm having this problem in more than one application. It is imperative I know how to push user input into a database. Any and all help is greatly appreciated.

Community
  • 1
  • 1
awimley
  • 692
  • 1
  • 9
  • 29
  • What does `console.log(req.headers['content-type'])` show if you put that inside your `addFlight()` method? – mscdex Jan 22 '15 at 21:07
  • it prints `application/x-www-form-urlencoded` – awimley Jan 22 '15 at 21:38
  • Ok, so if you temporarily put a middleware (`app.use(function(req,res,next) { console.dir(req.body); next(); })`) *right after* your `app.use(bodyParser.urlencoded())`, what does it show? – mscdex Jan 22 '15 at 22:11
  • it displayed 'undefined'. I added a role='submit' class to the button at the end of my form and now it works; so now I get my data array.... jade is finicky. – awimley Jan 22 '15 at 22:43

2 Answers2

3

Edit 1:

Have you tried verifying that the request is being sent with the HobbsOut value sent along with the POST http request and to the corrent url ('/logadd' in our case) using Fiddler?

Can you provide the generated form html instead of the jade template? because the jade templating engine itself has nothing to do with the problem you are facing. Your problem probably has to be one of the 3:

  1. The generated html is not correct.
  2. The url the form is posting the data is not the right one
  3. The server side failed to parse the data sent in the HTTP POST request.

Old Post:

You need to add support for parsing the data given inside the form by using bodyParser.urlencoded:

var express = require('express')
var bodyParser = require('body-parser')
var app = express()

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))

The last line adds form body parsing with content type of application/x-www-form-urlencoded.

The body-parser middleware parse the form body into server side variable req.body.

Community
  • 1
  • 1
Aviran Cohen
  • 5,581
  • 4
  • 48
  • 75
  • Well, I forgot to mention it because I didn't think it was relevant, but I do include `app.use(bodyParser.urlencoded({ extended: false }));` in my app.js file because it was suggested by another answer. – awimley Jan 22 '15 at 20:24
  • I checked it out, and apparently the POST request is not even being sent. Do you have any idea why? Sorry for the misinformed question, I'm very new to this. – awimley Jan 22 '15 at 21:29
0

Your action='' should probably be action='/logadd' if the user is not already at that URL (if they are, you can just leave out the action attribute altogether). Secondly, there is no native support for application/json HTML form submissions yet so you will need to use a application/x-www-form-urlencoded body parser middleware instead (or additionally).

mscdex
  • 104,356
  • 15
  • 192
  • 153
  • I have tried coding the action line both ways, and the user is at that URL. Also I can test the request directly with Postman, and it returns the same error. As stated in the comment above, I am already using both the JSON and urlencoded body parsers because another answer stated that you can use both. – awimley Jan 22 '15 at 20:31