Tuesday 16 May 2017

Check email exists in mysql database or not with nodejs

Need to check email is exists or not in mysql when user enters in the form. From form requser.email will have user emaild entered in form. How to compare requser.email with database to find match.

 router.post('/signup', function(req, res, next) {
    var requser = req.body.data.user;
  var hash = bcrypt.hashSync(req.body.data.user.password)
  var str = "";

  /*email check start*/

  /*email check end*/
  switch (requser.role) {
    case "approver":
      str = 'INSERT INTO users (name, email, password,role) values("' + requser.name + '","' + requser.email + '","' + hash + '","approver")';
      break;
    case "user":
      str = 'INSERT INTO users (name, email, password,role) values("' + requser.name + '","' + requser.email + '","' + hash + '","user")';
      break;
    case "admin":
      str = 'INSERT INTO users (name, email, password,role) values("' + requser.name + '","' + requser.email + '","' + hash + '","admin")';
      break;
  }
  if (!str) {
    return res.json({
      "status": "error",
      "data": {
        "redirect": "/"
      }
    });
  }
  connection.query(str, function(error, rows) {
    if (error) {
      console.log("error ocurred", error);
    }
    //console.log(rows);
    res.json({
      "status": error ? "error" : "success",
      "data": rows || ""
    })
  });
});



via Sri Sai Ramya T

No comments:

Post a Comment