FAQ: Learn Express Routes - Getting A Single Expression

@codeacademy This section is poorly explained. What these new terms,( route parameters, params object) mean, and how they are used really needs more clarity. The hint section needs to show the syntax. It would be much more clear if the exercise was analogous to the example, then we could follow the example to figure out the exercise.

This is the first time I’ve been completely lost on how to approach an exercise. :confounded:

3 Likes

Here’s a much better explanation of the content:

(Express Tutorial Part 4: Routes and controllers - Learn web development | MDN)

1 Like

In the util.js file there is the following code:

const getElementById = (id, elementList) => {
  return elementList.find((element) => {
    return element.id === Number(id);
  });
};

My question is is where does element in elementList.find((element) come from? There is no variable anywhere in the file that is element.

After looking at the other replies and questions I don’t feel so bad for not understanding what I was supposed to do in this exercise. Poorly explained. This needs revision.

3 Likes

yes - I am having trouble making it work on my local vs-code environment and keep seeing cannot Get/ even though I have tested it over and over on the machine and the code appears to be correct. It is a shame that it does not seem to work properly outside of the CA workspace. I am going to look through the discussions to see if there is any light shed. Will try this code too to see if it works: const { getElementById, seedElements } = require(‘./utils’); Just thought I would add my thoughts.

app.get(‘/expressions/:id’,(req,res,next)=>{
const foundExpression = getElementById(req.params.id,expressions)
res.send(foundExpression)
})

app.get(‘/expressions/:id’,(req,res,next)=>{
// const foundExpression = getElementById(req.params.id,expressions)
res.send(expressions[req.params.id])
})

so the top code works fine but the second snippet could you explain to me why when i run the id into the server machine it automatically selects the next item in the array, ex if i type 1 for the id it gives me back the second item in the array or if i type 2 it gives me 3 but i have no issues with that in the first code snippet

I guess because [req.params.id] is now acting as an array reference, so since array references start from 0:

res.send(expressions[0] gives the first item in the collection
res.send(expressions[1] gives the second item in the collection
res.send(expressions[2] gives the third item in the collection

I’m guessing that’s the reason…

following solution is shorter and sends the right response though not accepted…
why?

res.send(getElementById(req.params.id.substring(1), expressions));

btw the suggested solution does not work but i copied it over to continue the course

I think I got it… the lesson didn’t clearly show that the /pathtoitem/NUMBER should be instead of often showed /pathtoitem/:NUMBEr with collon is wanted

maybe this should have been done more clearly as it took me long to figure out as i do not like to see the solutions ever

1 Like

I came here because the example code was not explained properly:

const monsters = { 
  hydra: { height: 3, age: 4 }, 
  dragon: { height: 200, age: 350 } 
};
// GET /monsters/hydra
app.get('/monsters/:name', (req, res, next) => {
  console.log(req.params); // { name: 'hydra' }
  res.send(monsters[req.params.name]);
});

This had me confused, because the corresponding GET request is not explained or mentioned. Why is Hydra the one that is returned, and not dragon?

What was left out is that when making the GET request you specify ‘hydra’ as part of the request, hence you get data back about Hydra. The code “// GET /monsters/hydra” falls far short of explaining this! The corresponding GET request to use the code above properly is this:

axios.get('http://localhost:3000/monsters/hydra')

In short, don’t rely on Codecademy for a proper explanation, put the code into Chat GPT and get a proper explanation that you can ask follow up questions to!

I wouldn’t recommend relying on generative AI for answers. How can you verify the information you receive from CGPT? It’s better to use other resources—like documentation, books or actual peoples’ (S.O. for example) responses.

I understand your concern but think you answered your own question there! Ask, then check the answer against documentation and crowdsourced knowledge. The benefit of generative AI is that you can phrase a question in a way that can be hard when you don’t understand the topic of which you are asking about. Getting to the information you need in documentation or books is only easy when you can articulate search terms to find it. Generative AI’s conversational ability allows you to pose haphazard, half-formed questions (using example code) to narrow down your search.

I can’t understand this part in the example code

app.get('/monsters/:name', (req, res, next) => {
  console.log(req.params); // { name: 'hydra' }
  res.send(monsters[req.params.name]);
});

Why is it [req.params.name] instead of just [req.params]? doesn’t [req.params] already equals the value of name which is ‘hydra’?

If you use req.params, you get the object containing the route parameters:

console.log(req.params) // { name: 'hydra' }

If you use req.params.name , you get the value associated with the key name as a string from the req.params object:

console.log(req.params.name) // 'hydra'

1 Like

In this exercise : Getting A Single Expression

Show me something like “getElementbyId” but there isnt’t any explication about it, neither on internet. Have literally no sense for me as beginner, try to do something there isn’t on internet and trying to do as exercises without neither explained an example of something like that.

I’m finish this course only because i paid, but i don’t advise to no one!