首页 > 解决方案 > Node js,如何比较来自sql查询的两个结果?

问题描述

我的目标是比较两个sql结果,我尝试了很多东西,我已经想出了回调的使用,我的第一个想法是创建一个列表来存储两个回调的第一个参数和第二个参数列表,然后比较它们,但它没有,我对任何解决方案持开放态度,我的目标只是比较 sql 查询的结果,我的代码如下:

var mysql = require('mysql');
   
   
   
var con = mysql.createConnection({
  host: "localhost",
  user: "root",
  password: "",
  database: "espace_membre"
});
   
 var pseudo1 = "amine";
       
var socketid = '3485858585';
   
var peerId = '4444';
  


// 1. Call helloCatAsync passing a callback function,
//    which will be called receiving the result from the async operation
console.log("1. function called...")
helloCatAsync(function(result) {
    // 5. Received the result from the async function,
    //    now do whatever you want with it:
	
	var Finale = [];
	
	// console.log("5. result is: ", result);
	
Finale.push(result);

console.log(Finale);
	

	

});

// 2. The "callback" parameter is a reference to the function which
//    was passed as argument from the helloCatAsync call
function helloCatAsync(callback) {
    console.log("2. callback here is the function passed as argument above...")
    // 3. Start async operation:
    setTimeout(function() {
    console.log("3. start async operation...")
    console.log("4. finished async operation, calling the callback, passing the result...")
        // 4. Finished async operation,
        //    call the callback passing the result as argument
		con.connect(function(err) {
    
  con.query("SELECT gender FROM gender WHERE socketid = '" + peerId + "'", function (err, result) {
      
 
   
 

    callback(result);
   
  });
});

	con.connect(function(err) {

  con.query("SELECT gender FROM gender WHERE socketid = '" + socketid + "'", function (err, result) {
 
    callback(result);
	
  

  });
});
       
    }, Math.random() * 2000);
}

标签: javascriptsqlnode.jscallback

解决方案


推荐阅读