MySQL 如何将参数传递给nodejs中的mysql查询回调
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20819826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass parameters to mysql query callback in nodejs
提问by Julio
I'm trying to figure out the correct way of passing custom data to a query call to be made available in the callback. I'm using MySQL library in nodejs (all latest versions).
我试图找出将自定义数据传递给要在回调中可用的查询调用的正确方法。我在 nodejs(所有最新版本)中使用 MySQL 库。
I have a call to connection.query(sql, function(err, result) {...});
我打电话给 connection.query(sql, function(err, result) {...});
I couldn't find a way to 1) pass custom data/parameter to the call so that 2) it can be made available when the callback is invoked. So what is the proper way of doing so?
我找不到一种方法来 1) 将自定义数据/参数传递给调用,以便 2) 在调用回调时可以使用它。那么这样做的正确方法是什么?
I have the following (pseudo-code):
我有以下(伪代码):
...
for (ix in SomeJSONArray) {
sql = "SELECT (1) FROM someTable WHERE someColumn = " + SomeJSONArray[ix].id;
connection.query(sql, function (err, result) {
...
var y = SomeJSONArray[ix].id;
};
}
From the code above, I need to be able to pass the current value of "ix" used in the query to the callback itself.
从上面的代码中,我需要能够将查询中使用的“ix”的当前值传递给回调本身。
How do I do that?
我怎么做?
回答by Chris
If you are using node-mysql, do it like the docs say:
如果您使用的是 node-mysql,请按照文档中的说明进行操作:
connection.query(
'SELECT * FROM table WHERE id=? LIMIT ?, 5',[ user_id, start ],
function (err, results) {
}
);
The docs also have code for proper escaping of strings, but using the array in the query call automatically does the escaping for you.
文档也有正确转义字符串的代码,但在查询调用中使用数组会自动为您进行转义。
回答by sday
To answer the initial question with a complete answer/example to illustrate, wrap the callback with an anonymous function which immediately creates a scope containing a "snapshot" if you will of the data passed in.
要使用完整的答案/示例来回答最初的问题,请使用匿名函数包装回调,如果您愿意传入的数据,该函数会立即创建一个包含“快照”的范围。
var ix=1;
connection.query('SELECT 1',
(function(ix){
return function(err, rows, fields) {
console.log("ix="+ix);
console.log(rows);
};
})(ix));
For those new to this concept as I was 20 minutes ago, the last })(ix)); is the outer var ix=1 value which is passed into (function(ix){. This could be renamed (function(abc){ if you changed the console.log("ix="+abc);
对于那些刚接触这个概念的人,就像我 20 分钟前一样,最后一个 })(ix)); 是传入 (function(ix){. 这可以重命名 (function(abc){ 如果您更改了 console.log("ix="+abc);
fwiw (Thanks Chris for the link which filled in the blanks to arrive at a solution)
fwiw(感谢 Chris 提供的链接,它填补了空白以得出解决方案)
回答by bearvarine
While it is OK to pass variables or objects to a mysql query callback function using the tactic described earlier -- wrapping the callback function in an anonymous function -- I think it is largely unnecessary, and I'll explain why with an example:
虽然使用前面描述的策略将变量或对象传递给 mysql 查询回调函数是可以的——将回调函数包装在匿名函数中——但我认为这在很大程度上是不必要的,我将通过一个例子来解释原因:
// This actually works as expected!
function run_query (sql, y) {
var y1 = 1;
connection.query (sql, function (error, rows, fields) {
if (! error)
{
var r = rows[0];
console.log ("r = " + r[1]);
console.log ("x = " + x);
console.log ("y = " + y);
console.log ("y1= " + y);
console.log ("");
}
else
{
console.log ("error = " + error);
}
});
};
var x = 5;
console.log ("step 1: x = " + x);
run_query ("SELECT 1", x);
x = x + 1;
console.log ("step 2: x = " + x);
run_query ("SELECT 1", x);
x = x + 1;
console.log ("step 3: x = " + x);
Produces the following output:
产生以下输出:
step 1: x = 5
step 2: x = 6
step 3: x = 7
r = 1
x = 7
y = 5
y1= 5
r = 1
x = 7
y = 6
y1= 6
The fear is that the second call to run_query() will overwrite the variable y and/or y1 before the first call to run_query() has a chance to invoke its callback function. However, the variables in each instance of the called run_query() function are actually isolated from each other, saving the day.
令人担心的是,在第一次调用 run_query() 有机会调用其回调函数之前,第二次调用 run_query() 将覆盖变量 y 和/或 y1。但是,被调用的 run_query() 函数的每个实例中的变量实际上彼此隔离,从而节省了时间。