Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,143,411 members, 7,781,191 topics. Date: Friday, 29 March 2024 at 10:24 AM

Please I Need Explanation Of This Express.js Code - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Please I Need Explanation Of This Express.js Code (1287 Views)

I Need Explanation On This Code?? Php Programmers / Wrote My First JS Code On VS Code.... Lol (pics) / What Would Be The Output Of This JS Code (pics) (2) (3) (4)

(1) (2) (Reply) (Go Down)

Please I Need Explanation Of This Express.js Code by DDayve: 9:52pm On Jul 04, 2021
Please I need somebody to explain this code to me.
I was thinking that instead of using if (!found), I can as well use if (found===true). But the code didn't work when I used the later. Thanks in anticipation

Re: Please I Need Explanation Of This Express.js Code by ClintonNzedimma(m): 9:58pm On Jul 04, 2021
DDayve:
Please I need somebody to explain this code to me.
I was thinking that instead of using if (!found), I can as well use if (found===true). But the code didn't work when I used the later. Thanks in anticipation
What is the result of the code?

1 Like

Re: Please I Need Explanation Of This Express.js Code by bosman50(m): 2:10am On Jul 05, 2021
Your code reads
If not found that's why you have
If(!found)
Which is totally different from what you are looking for.
You don't use triple equal sign if you don't want to compare the data types along with comparing two values.
You can perhaps post where you used the code and the errors shown

2 Likes

Re: Please I Need Explanation Of This Express.js Code by Karleb(m): 5:00am On Jul 05, 2021
For most checks, == is okay.

The way to know a junior is when they check if a boolean is true or false in an if statement.

Rather than if(found == true) use if(found).
Rather than if(found == false) or if(found != true) use if(! found).
Re: Please I Need Explanation Of This Express.js Code by tope6217: 12:34pm On Jul 05, 2021
Any web dev can read this and understand
Re: Please I Need Explanation Of This Express.js Code by webapi: 8:52pm On Jul 06, 2021
DDayve:
Please I need somebody to explain this code to me.
I was thinking that instead of using if (!found), I can as well use if (found===true). But the code didn't work when I used the later. Thanks in anticipation
Why do you need a special boolean for this? Simply add an else and return your error code..
If (user.id===id){
return success;
}else {
return error;
}

2 Likes

Re: Please I Need Explanation Of This Express.js Code by Karleb(m): 9:55am On Jul 08, 2021
webapi:

Why do you need a special boolean for this? Simply add an else and return your error code..
If (user.id===id){
return success;
}else {
return error;
}

The boolean was really unnecessary there.
Re: Please I Need Explanation Of This Express.js Code by webapi: 1:33pm On Jul 08, 2021
Karleb:


The boolean was really unnecessary there.
I tire ooo
Re: Please I Need Explanation Of This Express.js Code by qtguru(m): 1:39pm On Jul 08, 2021
Karleb:
For most checks, == is okay.

The way to know a junior is when they check if a boolean is true or false in an if statement.

Rather than if(found == true) use if(found).
Rather than if(found == false) or if(found != true) use if(! found).


Double Sign is a bit dangerous it checks if the value are the same but not type , the === is more safe
e.g 2 == '2' evaluates as true but not in 3 equality

5 Likes

Re: Please I Need Explanation Of This Express.js Code by jikins(m): 12:12pm On Jul 09, 2021
webapi:

Why do you need a special boolean for this? Simply add an else and return your error code..
If (user.id===id){
return success;
}else {
return error;
}

If you understand how foreach works putting else inside the way you describe will give you bugs.

Foreach checks each item in the array. According to this your code if it checks the first item and the id don't match it returns error and that will be the end of its search. It won't go further than that because return stops the loop. So this just simply won't work at all.

Having this in mind you can begin to understand why the boolean of found was put inside to be changed to true only when the details matches. And the "!found" placed outside the foreach loop to catch the fact that if none of id matches found will never be changed to true.
Re: Please I Need Explanation Of This Express.js Code by jikins(m): 12:25pm On Jul 09, 2021
qtguru:


Double Sign is a bit dangerous it checks if the value are the same but not type , the === is more safe
e.g 2 == '2' evaluates as true but not in 3 equality


Exactly triple equals saves you from a lot of headaches. I always tell people just know that double equal exists but always use triple equals.
Re: Please I Need Explanation Of This Express.js Code by qtguru(m): 12:27pm On Jul 09, 2021
jikins:


Exactly triple equals saves you from a lot of headaches. I always tell people just know that double equal exists but always use triple equals.

Static type engineers will know this is an issue prob those not used to static type development might not see the issue. Which is why I prefer TS. Js is enough headache as it is
Re: Please I Need Explanation Of This Express.js Code by jikins(m): 12:34pm On Jul 09, 2021
qtguru:


Static type engineers will know this is an issue prob those not used to static type development might not see the issue. Which is why I prefer TS. Js is enough headache as it is

Typescript is a massive asset I swear.
Re: Please I Need Explanation Of This Express.js Code by webapi: 3:42pm On Jul 09, 2021
jikins:


If you understand how foreach works putting else inside the way you describe will give you bugs.

Foreach checks each item in the array. According to this your code if it checks the first item and the id don't match it returns error and that will be the end of its search. It won't go further than that because return stops the loop. So this just simply won't work at all.

Having this in mind you can begin to understand why the boolean of found was put inside to be changed to true only when the details matches. And the "!found" placed outside the foreach loop to catch the fact that if none of id matches found will never be changed to true.

Your point is completely wrong. Foreach is an iteration and it works per item/key. So if out of 10 objects of array, only 4 is true, then it will return true 4 times and false six times. It wont stop the iteration regardless of the condition you place. So back to my point, OP is completely wrong in his code.
Re: Please I Need Explanation Of This Express.js Code by qtguru(m): 4:21pm On Jul 09, 2021
webapi:

Your point is completely wrong. Foreach is an iteration and it works per item/key. So if out of 10 objects of array, only 4 is true, then it will return true 4 times and false six times. It wont stop the iteration regardless of the condition you place. So back to my point, OP is completely wrong in his code.

Exactly I noticed this the hard way earlier learning JS
Re: Please I Need Explanation Of This Express.js Code by webapi: 4:59pm On Jul 09, 2021
Here is a better solution for the OP since we are concerned about the items with user.id === id. Simple use the filter function to get the objects that matches the id then you send a success response.
Re: Please I Need Explanation Of This Express.js Code by jikins(m): 6:19pm On Jul 09, 2021
webapi:

Your point is completely wrong. Foreach is an iteration and it works per item/key. So if out of 10 objects of array, only 4 is true, then it will return true 4 times and false six times. It wont stop the iteration regardless of the condition you place. So back to my point, OP is completely wrong in his code.

You're right but my first statement still remains true. Using your initial code will still give you bugs.

Using your example the for each will give you true 4 times and false 6 times. So what if the last item in the array is false? The final result at the end will be false and not true. So the way it is written is much better than using else in there as it will always give you bugs except the final item is the true one
Re: Please I Need Explanation Of This Express.js Code by jikins(m): 6:21pm On Jul 09, 2021
webapi:
Here is a better solution for the OP since we are concerned about the items with user.id === id. Simple use the filter function to get the objects that matches the id then you send a success response.

Yes this is a better solution. Using else in foreach never works out well.
Re: Please I Need Explanation Of This Express.js Code by webapi: 6:25pm On Jul 09, 2021
jikins:


You're right but my first statement still remains true. Using your initial code will still give you bugs.

Using your example the for each will give you true 4 times and false 6 times. So what if the last item in the array is false? The final result at the end will be false and not true. So the way it is written is much better than using else in there as it will always give you bugs except the final item is the true one
Yes sorry i wasn't advising him to use return statement. I was only guiding him about the if and else statement. He can do whatever he likes within the conditions. But the filter is still the best option to be on safe side.
Re: Please I Need Explanation Of This Express.js Code by jikins(m): 6:32pm On Jul 09, 2021
webapi:

Yes sorry i wasn't advising him to use return statement. I was only guiding him about the if and else statement. He can do whatever he likes within the conditions. But the filter is still the best option to be on safe side.

Yes filter is better. But the code as it is in the picture will still work out fine as long as there is only one user with that id, which is normally the case.
Re: Please I Need Explanation Of This Express.js Code by webapi: 11:03pm On Jul 09, 2021
jikins:


Yes filter is better. But the code as it is in the picture will still work out fine as long as there is only one user with that id, which is normally the case.
For this case, Foreach wont work at all because expressjs can only send one response. If he sends any response on this condition, he will get an error message saying it had already sent the request. Filter is the only best option here. But if he must use Foreach, then he would need to use the push function to push the data into an empty array then send the array at once as the response
Re: Please I Need Explanation Of This Express.js Code by jikins(m): 1:08am On Jul 10, 2021
webapi:

For this case, Foreach wont work at all because expressjs can only send one response. If he sends any response on this condition, he will get an error message saying it had already sent the request. Filter is the only best option here. But if he must use Foreach, then he would need to use the push function to push the data into an empty array then send the array at once as the response

Not at all. The code as is sends only one response what ever the case may be. Its only if an else statement is placed inside thats when you will get multiple responses.

Look at the code again. If it finds a user with that id. It changes found to true. Then sends the response with the user data. The if statement outside the foreach won't fire because found is true. So essentially only one response is sent.

If none of the id match, the response in the foreach never fires and found is not changed from being false. So automatically, the if statement outside fires because found is false. So you see only one response fires in either case.

The only way multiple responses will fire is if more than one user have the same id. Which is very unlikely as the unique id of each user is the basis for their identification. So doing that just won't work at all for foreach, even filter will give you an array of all the user that matches that id and that's not obviously what you want to send.
Re: Please I Need Explanation Of This Express.js Code by webapi: 6:35am On Jul 10, 2021
jikins:


Not at all. The code as is sends only one response what ever the case may be. Its only if an else statement is placed inside thats when you will get multiple responses.

Look at the code again. If it finds a user with that id. It changes found to true. Then sends the response with the user data. The if statement outside the foreach won't fire because found is true. So essentially only one response is sent.

If none of the id match, the response in the foreach never fires and found is not changed from being false. So automatically, the if statement outside fires because found is false. So you see only one response fires in either case.

The only way multiple responses will fire is if more than one user have the same id. Which is very unlikely as the unique id of each user is the basis for their identification. So doing that just won't work at all for foreach, even filter will give you an array of all the user that matches that id and that's not obviously what you want to send.
Yes you are correct but nevertheless, the OP was not supposed to pull all the users from the database before iterating. I dont know what kind of database he's using but it's a wrong approach to pull every user from database before iterating. If there are 1million users, it wouldn't be nice to fetch all
Re: Please I Need Explanation Of This Express.js Code by jikins(m): 6:52am On Jul 10, 2021
webapi:

Yes you are correct but nevertheless, the OP was not supposed to pull all the users from the database before iterating. I dont know what kind of database he's using but it's a wrong approach to pull every user from database before iterating. If there are 1million users, it wouldn't be nice to fetch all

Not sure he is fetching anything to be honest. He is clearly on the path of learning node and express. Most likely he is using a static database object, that has a key of users which has an array of only a handful of users that he coded in.

He his obviously still learning and this is fine for now.
Re: Please I Need Explanation Of This Express.js Code by webapi: 7:36am On Jul 10, 2021
jikins:


Not sure he is fetching anything to be honest. He is clearly on the path of learning node and express. Most likely he is using a static database object, that has a key of users which has an array of only a handful of users that he coded in.

He his obviously still learning and this is fine for now.
Hopefully that is what he's doing
Re: Please I Need Explanation Of This Express.js Code by ugonna1054(m): 6:01pm On Jul 11, 2021
DDayve:
Please I need somebody to explain this code to me.
I was thinking that instead of using if (!found), I can as well use if (found===true). But the code didn't work when I used the later. Thanks in anticipation
Firstly,
If (!found) and if (found === true) are two different conditions, definitely the latter won’t work outside the forEach block because you already have similar condition inside the forEach block..

Secondly if you are using a MongoDB, you can use ODMs like mongoose or ORMs like sequelize in case of an SQL DB, and do sth like this instead model.findById(req.params.id); instead of having to loop through the entire user collection/table

1 Like

Re: Please I Need Explanation Of This Express.js Code by ugonna1054(m): 6:04pm On Jul 11, 2021
webapi:

For this case, Foreach wont work at all because expressjs can only send one response. If he sends any response on this condition, he will get an error message saying it had already sent the request. Filter is the only best option here. But if he must use Foreach, then he would need to use the push function to push the data into an empty array then send the array at once as the response
This is correct i.e if there are more than one users with the same id, it’s a bad practice nonetheless
Re: Please I Need Explanation Of This Express.js Code by Karleb(m): 6:27pm On Jul 11, 2021
ugonna1054:

Firstly,
If (!found) and if (found === true) are two different conditions, definitely the latter won’t work outside the forEach block because you already have similar condition inside the forEach block..

Secondly if you are using a MongoDB, you can use ODMs like mongoose or ORMs like sequelize in case of an SQL DB, and do sth like this instead model.findById(req.params.id); instead of having to loop through the entire user collection/table

I gave this exact solution but unfortunately, sperm boot hid it.

(1) (2) (Reply)

Jesus!!!! No Nigerian Dev Can Build This Type Of Website / Help Wit Project 'intelligent Data Analysis And Decision Support Systems' / First thing on your mind when asked to develop a website ?

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 48
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.