简体   繁体   中英

Node.js Registration Form

I am trying to implement a registration form into my web app. Once I insert the 'name' and 'email' provided by the user into my database (Mysql). I need separate error messages to pop up on the registration form if the 'name' or 'email' are already in use, something like

"the 'name' is already in use"

or

"the 'email' is already in use"

The problem is I use the error I get from the Mysql to show the message

"the 'name' or 'email' is already in use"

My question is how can i use the Mysql error to distinguish whether it is the 'name' or the 'email' that is being duplicated.

app.post(myArgs[0]+"/register", function(req,res){
      con.query('insert into users values(UUID(),\'' + req.body.user + '\',\''+req.body.pass+'\',\''+req.body.mail+'\',NULL)',function(err,res){
        if(err){
            console.error(err+"  Ooops name or email already in use !"); //here is where I want to find out whether it is the name or the email that is already in use! 
            errorregister();
          return;
        } else {
            passregister();
        }
      })
    function errorregister(){
      res.redirect(myArgs[0]+"/register");
      console.log(_dict);
    }
    function passregister(){
      res.redirect(myArgs[0]+"/login");
      console.log(_dict);
    }
  });

After the DB give you a error on inserting, you know that either email or name , or both, is duplicated.

You can then search the DB for name , then for email .

After that, customizing your error message is easy.

Checking before inserting could go wrong in case of race conditions (two users trying to register the same name/email at the same time). After inserting, you know for sure something is duplicated.

Check for error 1586 when inserting in MySQL( List of error codes )

You can check for which INSERT the error occurred.

Whenever a duplicate key is being inserted in a UNIQUE KEY column, this error is returned. Based on this error you can send a AJAX response to client, which would update DOM to show the required error.

Alright this is how i got it done

app.post(myArgs[0]+"/register", function(req,res){
      con.query('insert into users values(UUID(),\'' + req.body.user + '\',\''+req.body.pass+'\',\''+req.body.mail+'\',NULL)',function(err,res){
        if(err){
          error(err);
            return;
        } else {
            passregister();
        }
      })
      function error(err){
        if (err.toString().search("'PRIMARY'") != -1 ){
          console.error("  Ooops!!! name duplicated"); 
          res.redirect(myArgs[0]+"/register")
        } else {
            console.error("  Ooops!!! email duplicated");
            res.redirect(myArgs[0]+"/register")
          }
          return;
      }
    function passregister(){
      _dict[req.body.user]={};
      res.redirect(myArgs[0]+"/login");
      console.log(_dict);
    }
  });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM