Request.body is undefined

Adopting the tutorial to use request.body instead of request.query:

app.post("/dreams", function (request, response) {
  console.log(JSON.stringify(request.body));
  dreams.push(request.body.dream);
  response.sendStatus(200);
});```

```TypeError: Cannot read property 'dream' of undefined```

The log prints out "undefined". I'm sending in a body of ```{"dream":"Build something beautiful"}```
1 Like

By default it will be undefined, you need to use body-parsing middleware such as body-parser. See the explanation on http://expressjs.com/en/api.html#req.body or view a working version at https://hyperdev.com/#!/project/treasure-charger

4 Likes

Does this answer still hold? I seem to get an error trying to use body-parser

Error: Cannot find module ‘body-parser’

This happens any time I add

var bodyParser = require(‘body-parser’);

Have you included body-parser in the dependencies list in your package.json file?

I didn’t. Works now. Thanks for you help, I am a node newbie.

1 Like

Cool, glad that helped. Let us know any other probs you run into. I’m still learning this stuff too.