PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, July 13, 2022

[FIXED] How do I notify users, that they have entered dupilcate entry

 July 13, 2022     express, javascript, mongodb, node.js, web-deployment     No comments   

Issue

I'm using mongodb to store the details. I am aware that this question has already been asked, but the solutions did not work for me.

const userSchema = {
  email: {type: String, unique: true},
  password: String
};

I used the schema I entered above, and it kind of works, what I mean is, it doesn't let duplicate entries to be inserted into the database. But the problem I have is, that it doesn't get any error, so I'm not able to use the following code.

if(err){
  console.log("Repeated record");
}

Since there is no error, the page continues to load, for about 2-3 minutes, and finally it says "The page isn't working, localhost didn't send any data." Can someone please let me know what approach I should be taking to achieve this.

Edit: This is the server code:

app.get("/", function(req, res){
  res.render("home");
});

app.get("/login", function(req, res){
  res.render("login");
});

app.get("/register", function(req, res){
  res.render("register");
});

app.post("/register", function(req, res){
  const newUser = new User({
    email: req.body.username,
    password: req.body.password
  });
  newUser.save(function(err){
    if(err){
      console.log(err);
    }else{
      res.render("login");
    };
  });
});

app.post("/login", function(req, res){
  const username = req.body.username;
  const password = req.body.password;
  User.findOne({email: username}, function(err, foundUser){
    if(err){
      console.log(err);
    }else{
      if(foundUser){
        if(foundUser.password == password){
          res.sendFile(__dirname + "/public/upload.html");
        }
      }
    }
  });
});

I have installed express, body-parser and ejs. Home, login and register are templates of the extension .ejs .

Thank you.


Solution

You have to send response to client if error or something else occurred. Otherwise your function won't send any data. I added some comment lines into your code as marked as --> Here. Use res.send() or res.render() in these functions to send response to your client side.

Also adding status code may be helpful for understanding error code (just suggestion).

app.post("/register", function(req, res){
  const newUser = new User({
    email: req.body.username,
    password: req.body.password
  });
  newUser.save(function(err){
    if(err){
      console.log(err);
      // res.status(422).send(err) or res.render(errorPage) --> Here
    }else{
      res.render("login");
    };
  });
});

app.post("/login", function(req, res){
  const username = req.body.username;
  const password = req.body.password;
  User.findOne({email: username}, function(err, foundUser){
    if(err){
      console.log(err);
      // res.status(500).send(err) or res.render(errorPage); --> Here 
    }else{
      if(foundUser){
        if(foundUser.password == password){
          res.sendFile(__dirname + "/public/upload.html");
        } else {
          // res.status(401).send('Some error message') res.render(errorPage); --> And here
        }
      }
    }
  });
});


Answered By - Batuhan
Answer Checked By - Timothy Miller (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing