Can't complete get post challenge

Tell us what’s happening:
when I submit my phone I will see an error message can’t read the property or undefined first.
am on micro services and API.

/

solution: https://replit.com/@nanasv/boilerplate-express-2

Your browser information:

User Agent is: Mozilla/5.0 (Android 8.1.0; Mobile; rv:85.0) Gecko/85.0 Firefox/85.0.

Challenge: Get Data from POST Requests

Link to the challenge:

  1. body-parser middleware should be mounted before the route where you use it.
  2. check values’ names, they come from “name” attribute of form field in HTML
1 Like

Hello There!
I think your error might be here:

app.get("/name", function(req, res) {
  var firstName = req.query.first;
  var lastName = req.query.last;
   res.json({
    name: `${firstName} ${lastName}`
  });
});

Maybe the http method that you need isn’t GET, don’t you think?

GET - Read an existing resource without modifying it,
POST (sometimes PUT) - Create a new resource using the information sent with the request

1 Like

I used post HTTP and the error result is: CAN’T READ THE PROPERTY OF UNDEFINED FIRST.


var express = require('express');
var app = express();

var bodyParser = require('body-parser');



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


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

app.get("/name", function(req, res) {
  var firstName = req.query.first;
  var lastName = req.query.last;
  // OR you can destructure and rename the keys
  
  res.json({
    name: `${firstName} ${lastName}`
  });
});

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


app.use(function(req, res, next) {

var string = req.method + " " + req.path + " - " + req.ip;
 console.log(string);

  next();
});


app.get('json', function(req,res){
  var message = "Hello json";
  if(process.env.MESSAGE_STYLE === 'uppercase'){
return   res.json({
    "message": message.toUpperCase()
  });
  }
return res.json({"message": message});
});

app.get('/json', function(req,res){
  res.json({
    message: "Hello json"
  });
});

app.use('/public', express.static(__dirname + '/public'));
app.get('/', function(req,res){
  res.sendFile(__dirname + '/views/index.html');
});

app.get('/', function(req,res){
  res.send("Hello Express");
});

console.log('Hello World');



































 module.exports = app;

You see I actually check the name which have the attribute value of first,in HTML . And am using post HTTP.
The error message is still the same.

All right I have to use this setup before HTTP and its work

https://boilerplate-express-2.nanasv.repl.co

following here, it works

https://boilerplate-express-2.nanasv.repl.co

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