Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

196
Vistas
How do I notify users, that they have entered dupilcate entry

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.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

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
        }
      }
    }
  });
});
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda