Im stuck, need help badly

Tell us what’s happening:
Describe your issue in detail here.

The tests are not passing. if someone can help me point where the issue is…

Your project link(s)

solution: Glitch :・゚✧

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36

Challenge: Get Data from POST Requests

Link to the challenge:

The order of your code matters. You register POST /name before you register the bodyParser middleware. Therefore req.body is undefined when calling POST /name.

I’m sorry I didnt understand what you meant, today is my first day working with express and I dont understand half the stuff that I learnt so far. Could you please help me understand it better, Do you mean the variable bodyParser has to be initialized within the body of app.post?

At the moment you are first registering POST /name:

app.post("/name", function(req, res) {
  var string = req.body.first + " " + req.body.last;
  res.json({ name: string });
});

And after that you register the body-parser middleware:

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

In Express the middleware is run in the order you register it. So if you don’t register body-parser before POST /name you don’t have access to req.body (which is set by body-parser) inside the function which handles the POST request.

Thanks very much bud…I was able to fix the issue and definitely learnt something important…

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.