FAQ: Learn Express Routes - Using Queries

This community-built FAQ covers the “Using Queries” exercise from the lesson “Learn Express Routes”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Learn Express

FAQs on the exercise Using Queries

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Using Queries is a fraking wall. Before this part everything was relatively clear. But this. I just do not understand what is happening and even after hitting button “solution” I can’t deconstruc and understand how to get this result and what does it do at all. Very “interesting” way to teach people stuff.

14 Likes

I agree. I was lost here too.

I feel like the tutorial jumped (as in skipped some steps) and assumed we already knew how express worked.

I think if someone from codecademy performed a simple audit of this tutorial, they would agree… or not. I could just be dumb when it comes to Express.

5 Likes

I have to agree. The introduction of the new methods required (that become apparent when the solution is loaded) is missing in the lesson. The hints are not a lot of help and could perhaps be linked to an external resource.

3 Likes

The instructions are not clear in these lessons. The variables that are recommended and the logic that I am asked to perform are not clear or corollary. I feel frustrated that I have have the solution revealed just to understand what the instructions were directing me to do. There is some helpful information in here, but the Instructions need some more tweaking.

1 Like

My doubt here is I don’t find a way to make a request using any other verb different from get. I cannot find the ‘UPDATE’ tab. Can you tell me how can I activate / get it? Thanks.

Yeah, what happened here?

“You can use the updateElement helper function.”

What’s that?

3 Likes

I’m still new here, so I hope my ‘noob-view’ will help others like me out. :slight_smile:

first off, notice this on line 7 in app.js
const { getElementById, getIndexById, updateElement,
seedElements, createElement } = require(’./utils’); // this is where you’re importing the ‘updateElement’ helper function

then break down the provided text into bite-size chunks.
Use req.query to update the proper element in the expressions array.
use the updateElement helper function.
It takes three arguments

  1. id (id number of element)
  2. queryArguments (new, updated expressions object from req.query)
  3. elementList (array which contains the element to update.)
    returns updated element
    Which means it will look like this:

updateElement(id, query, elementList)
//which, in the function we’re using, becomes:
updateElement(req.params.id, req.query, expressions)

Explaining the solution

app.put(’/expressions/:id’, (req, res, next) => { //whenever a PUT-request is sent to the server with the following path: '/expressions/:id, start the following function

const expressionIndex = getIndexById(req.params.id, expressions); //getIndexById takes the id as first parameter, filename as second. the index is now assigned to ‘expressionIndex’.
with this we can check, in the code below, if the requested id exists.

if (expressionIndex !== -1) { //if expressionIndex DOES NOT equal -1, it means an index has been found, proceed with next two lines of code
updateElement(req.params.id, req.query, expressions); //now update the element
res.send(expressions[expressionIndex]);//in server response, send back the updated element

} else {
res.status(404).send(); //send 404 if expressionIndex was -1
}
});

Hope this helps!
Send me a message if you want me to change the formatting or have questions :slight_smile:

15 Likes

Thanks, stijndoeleman1050228!

For me, the trouble was understanding where updateElement() and getElementById() functions came from, as I kept googling and searching the code.

For the updateElement(), I had no idea what arguments that function accepted!

Thanks for the artful explanation as well!

I’m having trouble understanding how getIndexById works, could you explain it please?

1 Like

This lesson is garbage. The instructions do not provide enough scaffolding to ensure you know what you need to do. In a project it is different; throwing people into the deep end can be a good thing sometimes. But as a first exposure to a concept it is incredibly inefficient. The hints are also useless. Yes, I can read the solution and work my way backwards to how it makes sense, but I think that is not what codecademy is trying to do with their lessons.

1 Like

I understand it can be overwhelming, i had the exact same experience. This lesson is a good representation of programming as a job though.

You will have to look at code you do not understand, made by other humans, and try to figure it out. Asking questions and googling stuff is par for the course.

Understanding helper functions may seem tedious or annoying, but it is a skill that you will have to learn. I do, however, agree that the way it is presented here isn’t optimal.

Just keep on practicing, whenever learning feels hard is when you learn the most.

3 Likes

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

The find() method returns the value of the first element in an array that pass a test (provided as a function).

const getIndexById = (id, elementList) => {
return elementList.findIndex((element) => {
return element.id === Number(id);
});
};

The findIndex() method returns the index of the first element in an array that pass a test (provided as a function).

I think I understand how the find() and findIndex() functions work… but only up to the point where “-1” is returned when the Element or Index is not found. Why isn’t a value of “undefined” returned?

I have a question…
Does the updateElement( ) function prevents from SQL injection?
like anyone could temper the query string in the browser and in expressJS we directly pass that query string to update the element.

Great explanation, thanks : )

This lesson was designed to leave you banging your head against the wall!

You have to search through all the files, hints and google just to make sense of it all.

But it will finally make sense! Don’t give up! I head to go through the entire lesson twice before it started to click!

Wow I was so confused. Thanks for the step by step explanation it’s really missing from this part of the course.

You are right Stijn, but the reason I pay Codecademy for their lessons is that they ‘take me by the hand’, learning step-by-step. This lesson is freaking me out, leaving out of lot of necessary steps.

Overall Codecademy is a great platform, but they could improve some of the lessons to maintain a leading position (or other may overtake ;-)).

I agree with most of the students. Lessons in this section aren’t clear.