Request.body is undefined


#1

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"}```

#2

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


#3

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’);


#4

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


#5

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


#6

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