Basic Node and Express - Get Query Parameter Input from the Client - not passing

Tell us what’s happening:

I can’t seem to pass the test, or look for any relevant similar topics for my current problem, so I’m guessing it’s my current understanding of the topic.

Navigating to the /name?firstname=etc&lastname=etc URL is displaying what seems to be the correct functionality, a json object of: { ‘name’ : ‘first last’ } that is described in the challenge yet it doesn’t pass the test? What am I doing wrong?

Your code so far


app.get('/name', (req, res) => {
  let first = req.query.firstname;
  let last = req.query.lastname;
  
  let jsonObj = { name: `${first} ${last}` };
  res.send(jsonObj);
});

// I also tried this, as it was in the TIP section as it makes way for the upcoming POST challenges 
/**
app.route('/name').get((req, res) => {
  let first = req.query.firstname;
  let last = req.query.lastname;
  
  let jsonObj = { name: `${first} ${last}` };
  
  res.send(jsonObj);
}).post();
**/

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/apis-and-microservices/basic-node-and-express/get-query-parameter-input-from-the-client

I realized that my mistake was misunderstanding the query name. I put ‘firstname’, and ‘lastname’ rather than just ‘first’ and ‘last’

2 Likes

I tried using these codes and they’re not passing either:
Code 1:

app.get("/name", function(req, res) {
   var firstName = req.query.first;
   var lastName = req.query.last;
   var jsonObj = {name: 'firstName lastName'};
   res.send(jsonObj);
 });

Code 2:

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

Code 3:

app.get('/name', (req, res) => {
   var first = req.query.first;
   var last = req.query.last;
   var jsonObj = {name: '${first} ${last}'};
   res.send(jsonObj);
 });

I get the same response when I enter my URL into FCC:
“GET /name” route does not behave as expected
Your API endpoint should respond with the correct name

What am I doing wrong?

Can we have a look at your glitch project?

In the second try it’s wrong how you populate the variables, ‘firstname’ belong to the property ‘first’ in the query, so you find it in req.query.first. It’s the same for ‘last’.
When your variable are ready, you have to modify jsonObj to {"name": first + ' ' + last},
that’s all!

2 Likes

https://newp.glitch.me

I had a problem with the original project so I had to start a new one. All my commented out codes were lost.

It works. Here is the code:

app.route('/name').get((req, res) => {
   var first = req.query.first;
   var last = req.query.last;
   var jsonObj = {name: first + ' ' + last};
   res.send(jsonObj);
 }).post();

Thank you very much.

6 Likes

Sorry for being late. I just reviewed the snippets and you are using single quotes instead of the tilde (`). If you use ${var} you must use tildes.

This code is also working. Even without using the .route and .post methods:

app.get('/name', function(req, res){
  let first = req.query.first;
  let last = req.query.last;
  let jsonObj = {name: first + " " + last};
  res.send(jsonObj);
});
 

you have to resend with res.json() instead of res.send()

Hey folks,

I thought I’d follow up as this was my first google hit that came up when I was having the same problem. I realize now that it’s a silly issue, so thought it would help to clear it up.

The FCC testing on this particular challenge (as of August 2019) uses SPECIFIC query strings. The pass/fail mechanism has nothing to do with res.json VS res.send or using res.get VS res.route.

Hint: look closely at the example they use and make sure you are using the same exact query variables!

happy coding