javascript - 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);
}
解决方案
推荐阅读
- r - 如何从数值向量创建一个函数,该函数将一个值作为输入并输出该值在向量中出现的概率?
- heroku - 谷歌云 DNS 子域映射到 Heroku
- c# - 使用 CollectionView 时 Xamarin 应用程序在 android 上崩溃
- python - 打包一个需要非python包的python项目
- git - 如何干净地删除包含 .gitignore 并可能包含临时忽略文件的目录?
- c++ - 如何解压缩霍夫曼编码文件?
- assembly - 打印可被输入 1 - 9 整除的 0 - 255 的所有数字
- scala - 过滤数据框中的日期范围
- r - 制作 ggplot 0,0 XY 坐标以与背景图像的特定点重合
- sql - 删除不起作用的地方