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

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

#1

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


#2

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


#4

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?


#5

Can we have a look at your glitch project?


#6

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!


#7

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.


#8

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.


#9

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.


#10

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);
});