Node.jsでデータコンセプトを渡すのに苦労しています。 例としてSQLを面倒にしましょう。ここでは例のコードは次のとおりです。Node.jsで定義済みの関数を使用して引数を渡す
//acquire a connection
pool.acquire(function poolAcquire(err, connection) {
if (err) { console.error(err); return; }
//use the connection as normal
var request = new Request('select 1;select 2;select 3', function requestCallback (err, rowCount) {
if (err) { console.error(err); return;}
console.log('rowCount: ' + rowCount);
//release the connection back to the pool when finished
connection.release();
});
request.on('row', function onRequestRow(columns) {
console.log('value: ' + columns[0].value);
});
connection.execSql(request);
});
pool.acguireは
私の質問があり、引数としての機能を取り、この機能は、特定の署名を持っている(ERR、接続) - どのように私は、SQL文を渡しますこの関数の中に? 異なる関数の署名が呼び出されないため、署名を変更できません。
また、変数が外部で変更される可能性があるため、グローバルスコープは使用できません。
つまり、私はラッパー呼び出しをバイパスしていくつかのデータを渡す方法を見つける必要があります。
別の関数でラップすることにより
var mySQLs = ['select 1;select 2;select 3','select 4;select 5;'];
async.forEach(mySQLs,WorkWithOneItem, AllIsDone);
function WorkWithOneItem(item, callback){
pool.acquire(?????(item));
callback(); // tell async that the iterator has completed
}
function AllIsDone (err) {
console.log('All done');
}