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.
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
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!
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();
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);
});
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!