Click here to Skip to main content
15,886,835 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi! I am trying to create an app and trying to filter the handmade database if it has already signed up email.

My Code:
//My Handmade database (empty)
const users = [];


formRegister.post("/register", (req, res) => {

    const user = req.body;

    //If the inputs are empty strings.
    if (validateFunction(user.email, user.password) === 400) {
        res.status(400).send({
            message:"Cant Be Empty!",
            success:false
        });
    } else {

        //if we have already have the email
       if(users.filter(user=>user.email === users.email)){
           res.status(400).send({
            message:"This email already registered!",
            success:false
           });

           //If it is not empty and not already in database,created
       } else{

        users.push(user);
        res.status(200).send(`
        <body style="overflow-y:hidden;">
            <div style="height:100vh;display:flex;justify-content:center;align-items:center;flex-direction:column">
                <h1 style="color:#00C24E font">SUCCESS</h1>
                <h4 style="color:#DC3545">${user.email}</h4>
                <p>Has added to database succesfuly.</p>
                <a href="/getData">Get Other Users</a>
            </div>
        </body>`)
       }
    }

});



I am so confused about the filter part. Anyone can help me, please?

What I have tried:

if(users.filter(user=>user.email === users.email)){
Posted
Updated 25-Jan-21 23:47pm
Comments
Christian Graus 21-Jan-21 21:59pm    
What do you want to do? Have you set breakpoints to see what this code does?

Users is a collection and user in this function will be each user in the collection, not the user you set above
slex1one-Musdy 22-Jan-21 5:06am    
The point is, I cant filter my object if the email already exists in it. This Array prototype confused me.

If email already exists in the object it should send 400 status else it should be created and send 200 status.

Christian Graus 22-Jan-21 16:22pm    
So this code should iterate through your collection and yes, return a 400 if the email is in it. If it's not working, I'd put that code on a new line, and debug it in Chrome.

1 solution

const users = [];
formRegister.post("/register", (req, res) => {
  const currentUser = req.body;

  //If the inputs are empty strings.
  if (validateFunction(currentUser.email, currentUser.password) === 400) {
    res.status(400).send({
      message: "Cant Be Empty!",
      success: false,
    });
  } else {
    const useExists = users.filter((user) => user.email === currentUser.email);
    //if we have already have the email
    if (useExists.length > 0) {
      res.status(400).send({
        message: "This email already registered!",
        success: false,
      });

      //If it is not empty and not already in database,created
    } else {
      users.push(user);
      res.status(200).send(`
	<body style="overflow-y:hidden;">
		<div style="height:100vh;display:flex;justify-content:center;align-items:center;flex-direction:column">
			<h1 style="color:#00C24E font">SUCCESS</h1>
			<h4 style="color:#DC3545">${user.email}</h4>
			<p>Has added to database succesfuly.</p>
			<a href="/getData">Get Other Users</a>
		</div>
	</body>`);
    }
  }
});
 
Share this answer
 
Comments
CHill60 26-Jan-21 12:18pm    
A code dump does not make a good solution. It would be more helpful to explain the differences between your code and the OPs and why they are different.
For example: Why is there a comma at the end of lines 10 and 18?
Why is "=== user.email).length" better than "=== users.email"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900