Big pit encountered in acquiring mysql data by NODE.js synchronous call

Keywords: Front-end Session SQL Database MySQL

Today's edition will share with you an article about the pit encountered when nodejs synchronous invocation acquires mysql data. It is very comprehensive and meticulous. It has a certain reference value. Friends who need it can refer to and learn from it. If there are any shortcomings, criticism and correction are welcome.

mysql calls to get data can only return results asynchronously, but not synchronously. Therefore, it is necessary to write processing events in callback functions. During this period, I looked at Aysnc.js, which is used for multiple events to return callback functions. These events are organized in an orderly way, and finally only one callback function is returned. It does not change the nature of asynchrony, but integrates multiple asynchronism into one asynchronism, so as to meet the needs of writing programs.

Wrong demonstration

Getting Data Functions in the Database

var _getUser = function(name) {
  var sql = "SELECT * FROM " + TABLE + " WHERE user_loginname='" + name + "'";
  connection.query(sql, function(err, results) {
    if(!err) {
      var res = hasUser(results);
      return res;
    }else {
      return error();
    }
  });
  function hasUser(results) {
    if(results.length == 0) {
      return {err: 1, msg: "This username does not exist"};
    }
    else {
      return results[0];
    }
  }
  function error() {
    return {err: 1, msg: "Database error"};
  }
}
var getUser = function(name){
  return _getUser(name);
}//Here I recommend a front-end full stack development and exchange circle: 619586920 breaks through the technical bottleneck and improves the thinking ability.

Get results to process events

//Get the uname value from the data data data on the post
var uname = req.body.uname; 
var User = getUser(uname);
if(User.err){
   res.status(404)
 } else {
  var upwd = md5 (req.body.upwd);
  //Information matching user names is queried, but the corresponding password attribute does not match
  if(upwd != User.user_passwd){  
     req.session.error = "Password error";
     res.send(404);
     // res.redirect("/login");
   }else{ 
     //If the information matches successfully, this object (matched user) is assigned to session.user and returned to success.                  
     req.session.user = {name: uname, password: upwd};
     res.status(200).send("success")
     // res.send(200);
     // res.redirect("/home");
   }
 }
// md5 encryption
function md5 (text) {
  return crypto.createHash('md5').update(text).digest('hex');
};//Here I recommend a front-end full stack development and exchange circle: 619586920 breaks through the technical bottleneck and improves the thinking ability.

Correct demonstration

Getting Data Functions in the Database

var _getUser = function(name, callback) {
  var sql = "SELECT * FROM " + TABLE + " WHERE user_loginname='" + name + "'";
  connection.query(sql, function(err, results) {
    if(!err) {
      var res = hasUser(results)
      callback(res);
    }else {
      callback(error());
    }
  });
  function hasUser(results) {
    if(results.length == 0) {
      return {err: 1, msg: "This username does not exist"};
    }
    else {
      return results[0];
    }
  }
  function error() {
    return {err: 1, msg: "Database error"};
  }
}
var getUser = function(name, callback){
  return _getUser(name, callback);
}//Here I recommend a front-end full stack development and exchange circle: 619586920 breaks through the technical bottleneck and improves the thinking ability.

Get results to process events

//Get the uname value from the data data data on the post
var uname = req.body.uname; 
getUser(uname, function(data){
  var User = data;
  if(User.err){
    res.status(404)
  } else {
    var upwd = md5 (req.body.upwd);
    //Information matching user names is queried, but the corresponding password attribute does not match
    if(upwd != User.user_passwd){  
      req.session.error = "Password error";
      res.send(404);
      // res.redirect("/login");
    }else{ 
      //If the information matches successfully, this object (matched user) is assigned to session.user and returned to success.                  
      req.session.user = {name: uname, password: upwd};
      res.status(200).send("success")
      // res.send(200);
      // res.redirect("/home");
    }
  }
});//Here I recommend a front-end full stack development and exchange circle: 619586920 breaks through the technical bottleneck and improves the thinking ability.
// md5 encryption
function md5 (text) {
  return crypto.createHash

epilogue

Thank you for watching. If there are any shortcomings, you are welcome to criticize and correct.

Posted by rob323 on Sun, 31 Mar 2019 01:51:30 -0700