Get Data from POST Requests

I have completed this lesson (my code passes) but have a question: When I press Submit on the HTML section, it displays John Doe very quickly and then shows {“name”:“undefined undefined”}. Why is this?

Here is my code:

Spoiler
const mySecret = process.env['MESSAGE_STYLE']
let express = require('express');
let bodyParser = require('body-parser');
let app = express();
console.log('Hello World');
//app.get('/', (req, res) => res.send('Hello Express'));

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

app.get('/', (req, res) => res.sendFile(__dirname + '/views/index.html'));

app.use('/public', express.static(__dirname + '/public'));
app.use((req, res, next) => {
  console.log(req.method + " " + req.path + " - " + req.ip);
  next();
});

app.get('/json', (req, res) => res.json(process.env.MESSAGE_STYLE == 'uppercase' ? {"message": "HELLO JSON"} : {"message": "Hello json"}));

app.get('/now', function(req, res, next) {
  req.time=new Date().toString();
  next();
  },
  function(req, res, next) {
    res.json({"time": req.time});
  }
);

app.get("/:word/echo", (req, res) => {
  res.json({
    "echo": req.params.word
  });
});

app.get("/name", (req, res) => {
  const firstname=req.query.first;
  const lastname=req.query.last;
  res.json({"name": `${firstname} ${lastname}`});
});

app.post("/name", (req, res) => {
  const firstname=req.body.first;
  const lastname=req.body.last;
  res.json({"name": `${firstname} ${lastname}`});
});

module.exports = app;

This is the lesson: https://www.freecodecamp.org/learn/back-end-development-and-apis/basic-node-and-express/get-data-from-post-requests

Thanks
Alan

If you don’t get much response it might be because you haven’t provided a link to the challenge or exercise you are talking about…

Thanks - I’ve amended my original post

It seems to only happen in the embedded view. If you click the “Open in new tab” button to the right of the address bar and do it in the full page view it doesn’t happen.

It is doing a GET after the POST to the same route /name in the embedded view. That route already exists as part of the Query Parameter challenge and is looking for a query that isn’t sent with that GET request. So the query params are missing and the values are undefined, which is then res.jsoned as part of that route.

Thank you for the explanation