Why check if an id exists in the expressions array?

Question

Why check if an id exists in the expressions array?

Answer

We might think that if the id parameter does not exist in our array, by default it will just not update anything, and in a way we are correct, but it will still try and it will return a 200 ok. For example, there is no expression with id 20, if we run the server we can get 1:
06%20AM 15%20AM

Yet, if we try as well with an id of 20:
32%20AM

OF COURSE, as expected! Now, if we just set up our PUT request to update the array without checking if the item already exists, we can update the first item:
48%20AM

the problem comes when trying to update with the id of 20:
26%20AM

It somehow shows success , but two things:
One, the item does not exist, and two, it wasn’t really updated.

PUT does have to create capabilities, but since the expressions are items in an array, an array (in JavaScript) cannot get a new item without using the .push() method, so the assignment is not successful but the operation and succeeding actions are still performed, returning a 200 Ok success response.

Therefore, the main reason for us to check if an id exists before any action is been performed is that otherwise, it would become an expensive transaction. No matter how fast a computer is, time is still of the escence, in this case, yes the user might receive the data in time, but it is an update that never happened, so the user just wasted some time and it makes it to be a bug in our code, while also thinking that any proccess in a hosted server is usually either charged by the operation or by the time an operation takes, then the fact that we are running a function regardless if the item even exists will costs us (or the company we work for) real money every time. We want to be effective, and we want our API method request handlers to be as efficient as possible. That is why, it is important to check for values received in the query if they are the type needed for the action, and if the item requested exists.

5 Likes

How come we need to do the whole

 const expressionIndex = getIndexById(req.params.id, expressions)

thing ? Why if we do simple

expression[req.params.id] = req.query

it won’t work?

I suppose the syntax of JS doesn’t allow such manipulation. That’s why we would have to use the helper function that correctly pops and pushes the information appropriately.

Js syntax offers such type of manipulation. But the logic you are trying to use is not appropriate: expression[req.params.id] = req.query .you need to first check if req.params.id is valid or not.For e.g req.params.id can be anything like(1,2,“abc”,"#2$" etc) and if you try apply expression["abc"] = req.query that would be invalid. so you need to first check if an ID is valid or not.Moreover if a Id even exist or not.

I don’t understand the need for getIndexById() in this exercise. Could we not check

if (expressions[req.params.id]) { // Update the expression }

and, rather than retrieve the index, return

res.send(expressions[req.params.id]);?

getIndexById( ) Validates the ID which we have received in the request before performing any operation(like creating,retrieving,updating etc)

Yes, I understand that. Does if (expressions[req.params.id]) not achieve the same effect?

Yes, if the id is valid & No, if the Id is Invalid.
Example:

expressions=[:grinning:, :clown_face:, :grin:, :laughing: ,:sweat_smile:]
if we have Get a Request something like:

Get /expressions/1 (Here req.params.id is equal to 1)
expressions[req.params.id] // :clown_face:

Get /expressions/6 (Here req.params.id is equal to 6)
expressions[req.params.id] //undefined

Therefore It is recommended here to Validate the id before performing Any Operation.

1 Like

Since undefined is not truthy, I still don’t understand why if (expressions[req.params.id]) doesn’t suffice to validate the id…

In your example, Get /expressions/6 run through if (expressions[req.params.id]) = false and thus the operation won’t run, no? What have I missed here?

1 Like

I realized my earlier provided example was very inefficient.

i hope this Example would help :

expressions=[ { id: 101 , emoji : 🥶 } , { id: 102 , emoji : 🤯 } , { id: 103 , emoji : 😷 } ]

Now suppose if we have Get a Request something like:

Get /expressions/2 Here the id is 2

if (expressions[req.params.id]) { // Perform the operation }
This logic is wrong because here the if statement will be truthy for id=2 even though we don’t have any expressions with id = 2.

So a proper solution to this issue would be fetching the index first with the help of req.params.id and then proceed with any requested operation.

Hence in the lesson const expressionIndex = getIndexById(req.params.id, expressions) is used.

5 Likes

That makes more sense, thank you!

2 Likes

‘Id’ is not an index number - it is an object’s key as in…

{
id: ...,
name:...
}

You can verify it by checking the utils.js file.

For instance if req.params.id === 1 your line expression[req.params.id] would return the second object in the expression array, who’s ‘id’ is 2.

Also keep in mind that:

  • objects aren’t necessarily positioned in sequential order matching id numbers,
  • id’s could include letters and other symbols.