Get Query Parameter Input from the Client not passing

my code still not passing, despite carefully following the instructions
code: app.get(’/name’, (req, res) => {
let firstname = req.query.first;
let lastname = req.query.last;
res.json({ “name”: firstname + " " + lastname })
});

1 Like

Are you not retrieving loading the page first?

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

that should be from prev. exercises

In order to send the data, they expect that page with a form.


Just one other suggestion, use destructuring for the req.query object.

i also followed the exact example from the lesson illustration
code: app.get(’/name’, (req, res) => {
const {"/name?first=firstname&last=lastname"} = req.query;
res.json({“name”: ‘firstname lastname’});
});
still not passing

its already in the code from the prev lessons, and what do you mean “expect the page with a form”…and thank you for taking ur time to respond

nvm. You may include the link to your repl so someone can take a better look at the code and help you out…

here it is https://replit.com/@6ix-Ville/boilerplate-express#myApp.js

my code still not after several hours of browsing github and stackoverflow…is this a glitch from FCC? im deeply worried. pls any help
code: app.get(’/name’, (req, res) => {
let first = req.query.first;
let last = req.query.last;

res.json({
name: ${first} ${last}
});
});

It is not, I’ve checked out your page and with a simple fix it passes, so you’re very close. Getting stuck is always beneficial.

There is an error here:

app.get('/name', (req, res) => {
  var { firstname: firstname, lastname: lastname } = req.query;
  res.json({
    name: `${firstname} ${lastname}`
  });
  });

You could do some console logs and get what is going on. But let me give some context & background in case it helps.

context & background

Let’s see again what the exercise asks for:

  • Respond to a GET request landing with params first and last, with a json object

A get request, if it sends any query parameters (as a way to send information to a server like in a google search). It sends them with this format:
www.google.com/?param1=value1&param2=value2&...
where google.com will instead be any url, in your case, it’s your repl.it public url (the webpage).

They are sending this url with query parameters:

  • yourUrl/?first=name&last=surname
    where name and surname could be coming from the form, with a GET method.

The exercise

Alright, then, your data should be like this, in the req.query object:

{
first:"name",
last:"surname
}

here is where the error comes in.

How do you destructure that object?

thanks a bunch ive solved it using
code: app.get("/name", (req, res) => {
let str = req.query.first + " " + req.query.last ;
res.json({name: str})
});