6

I've got a simply form, that sends POST data to my Node.JS server, with Express. This is the form:

<form method="post" action="/sendmessage">
  <div class="ui-widget">
      <input type="text" id="search" data-provide="typeahead" placeholder="Search..." />
  </div>
  <textarea id="message"></textarea>
</form>

The ui-widget and the input is releated with typehead, an autocomplete library from Twitter. And this is how I handle the POST request in the server:

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

  console.log(req.body);

  usermodel.findOne({ user: req.session.user }, function (err, auser){

        if (err) throw err;

        usermodel.findOne({ user: req.body.search }, function (err, user){

            if (err) throw err;

             var message = new messagemodel({

                  fromuser: auser._id,
                  touser: user._id,
                  message: req.body.message,
                  status: false

             });

             message.save(function (err){

                  if (err) throw err;

                  res.redirect('/messages')

             })

        });

   });

});

The console shows me '{}', and then an error with req.body.search, because search is not defined. I don't know what is happening here, and it's not a problem related with the typehead input. Any solution for this problem...?

Thank's advance!

MrMangado
  • 993
  • 4
  • 10
  • 26
  • I would add in console checks everywhere to check what `req.body` is outputting as well, if `req.body` is empty then I would look at what is wrong with how/what you are passing to `/sendmessage` – germainelol Mar 10 '13 at 13:28
  • Try adding a `name="search"` attribute to your input field. – JohnnyHK Mar 10 '13 at 14:12

5 Answers5

22

req.body is made up of names and values.

add name="search" on your search box and try again.

You also must use the express/connect.bodyParser() middleware, thanks Nick Mitchinson!

Ari Porad
  • 2,834
  • 3
  • 33
  • 51
  • 2
    Also, I would ensure that you have used the Express/Connect bodyParser. If not, Express will not populate the req.body object. – Nick Mitchinson Mar 10 '13 at 18:25
  • 1
    I was banging my head for why, my request.body is empty and then realized, that my inputs should have name. Classic fool :D. Thank you.. – Jeremy Rajan Mar 25 '16 at 17:41
11

I had this problem and it turned out I was using app.use(express.bodyParser()); but it was after the code I was using. Moving it up solved the issue.

wesbos
  • 25,839
  • 30
  • 106
  • 143
  • 7
    Very important answer - Express is very finnicky about location of anything in the app.use() configuration object. I encountered the problem where app.use(express.bodyParrser()) didn't work because I was declaring my routes from an external file, and initializing them before calling app.use(express.bodyParser()) – netpoetica Aug 03 '13 at 13:12
  • 2
    Thanks, this was exactly my issue :) – alvarodms Mar 15 '16 at 13:32
6

on express 4 would be this one. (note that this will not parse multipart/uploads).

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

and if you want to receive json input

app.use(bodyParser.json());
Jorge Avila
  • 71
  • 1
  • 5
0

In my case it was caused by my app redirecting http to https. Updating Facebook to use the https uri fixed it.

webjay
  • 5,358
  • 9
  • 45
  • 62
0

In my case I did not provide name tags like this name="firstname" in my input field. After adding them, everything worked.

apilot
  • 1
  • 1