SoFunction
Updated on 2025-03-10

A big pit encountered when nodejs synchronous call to obtain mysql data

MySQL calls to obtain data can only return results in an asynchronous manner, and cannot obtain results synchronously. Therefore, you must write processing events in the callback function. During this period, I looked at it and used multiple events to return callback functions, organize these events in an orderly manner, and finally return only one callback function. It does not change the essence of asynchronousness, but integrates multiple asynchronously into one asynchronous, thereby meeting the needs of writing programs.

Error demonstration

Get data functions in the database

var _getUser = function(name) {
  var sql = "SELECT * FROM " + TABLE + " WHERE user_loginname='" + name + "'";
  (sql, function(err, results) {
    if(!err) {
      var res = hasUser(results);
      return res;
    }else {
      return error();
    }
  });
  function hasUser(results) {
    if( == 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);
}

Get results to process events

//Get the value of uname in the data data from the postvar uname = ; 
var User = getUser(uname);
if(){
   (404)
 } else {
  var upwd = md5 ();
  //Query the information matching the username, but the corresponding password attribute does not match  if(upwd != User.user_passwd){  
      = "Error password";
     (404);
     // ("/login");
   }else{ 
     //The information matching is successful, then assign this object (matched user) to and return successful      = {name: uname, password: upwd};
     (200).send("success")
     // (200);
     // ("/home");
   }
 }
// md5 encryptionfunction md5 (text) {
  return ('md5').update(text).digest('hex');
};

Correct demonstration

Get data functions in the database

var _getUser = function(name, callback) {
  var sql = "SELECT * FROM " + TABLE + " WHERE user_loginname='" + name + "'";
  (sql, function(err, results) {
    if(!err) {
      var res = hasUser(results)
      callback(res);
    }else {
      callback(error());
    }
  });
  function hasUser(results) {
    if( == 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);
}

Get results to process events

//Get the value of uname in the data data from the postvar uname = ; 
getUser(uname, function(data){
  var User = data;
  if(){
    (404)
  } else {
    var upwd = md5 ();
    //Query the information matching the username, but the corresponding password attribute does not match    if(upwd != User.user_passwd){  
       = "Error password";
      (404);
      // ("/login");
    }else{ 
      //The information matching is successful, then assign this object (matched user) to and return successful       = {name: uname, password: upwd};
      (200).send("success")
      // (200);
      // ("/home");
    }
  }
});
// md5 encryptionfunction md5 (text) {
  return ('md5').update(text).digest('hex');
};

Summarize

The above is the entire content of this article. I hope that the content of this article has certain reference value for your study or work. Thank you for your support. If you want to know more about it, please see the relevant links below