2016-08-31 10 views
0

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'); 
} 

答えて

2

ような何か:

function aquire(sql, callback) { 
    pool.acquire(function poolAcquire(err, connection) { 
     if (err) { console.error(err); return callback(); } 

     //use the connection as normal 
     var request = new Request(sql, 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(); 
      callback(); 
     }); 

     request.on('row', function onRequestRow(columns) { 
      console.log('value: ' + columns[0].value); 
     }); 

     connection.execSql(request); 
    }); 
} 

function WorkWithOneItem(item, callback){ 
    acquire(item,() => { 
     callback(); // tell async that the iterator has completed 
    }); 
} 
1

あなたにも出て結果が必要ですか?

var mySQLs = ['select 1;select 2;select 3','select 4;select 5;']; 
async.forEach(mySQLs, WorkWithOneItem, AllIsDone); 

function WorkWithOneItem(sql, callback){ 
    pool.acquire(function poolAcquire(err, connection) { 
     if (err) return callback(err); 

     //use the connection as normal 
     var request = new Request(sql, function requestCallback (err, rowCount) { 
      if (err) return callback(err); 

      console.log('rowCount: ' + rowCount); 

      //release the connection back to the pool when finished 
      connection.release(); 
     }); 

     var rows = []; 
     var count = 0; 
     request.on('row', function onRequestRow(columns) { 
      console.log('value: ' + columns[0].value); 
      rows.push(columns[0].value); // or whatever you actually want out of these. 
     }); 

     request.on('done', function onRequestDone() { 
     callback(null, rows); 
     }); 

     connection.execSql(request); 
    }); 
    callback(); // tell async that the iterator has completed 
} 

function AllIsDone (err) { 
    console.log('All done'); 
    // you probably want async.map, so you can get the result back 
    // as the second argument for a function like this 
} 
関連する問題