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:

2 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.

2 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…