2013-03-12 125 views
0

我對nodejs很新穎。如何阻止查詢結果的nodejs

我希望我的代碼能夠多次查詢數據庫,從一個變量中的所有查詢中收集數據,然後在某處使用它。

但我猜nodejs而不是等待查詢的結果,執行沒有阻塞。 這是我認爲正在發生的事情。對不起,如果我錯了。

for (var i = step_min; i < (step_max); i += step) { 
    query = 'select count(' + colName + ') as num_count from ' + 
     rows[0].tablename + ' where ' + 'dictionaryid=' + 
     rows[0].dictionaryid + ' and ' + colName + ' between ' + i + 
     ' and ' + (i + step); 

    connection.query(query, function(err, rows1, fields) { 
     if (err) { 
      console.log(err); 
      throw err; 
     } 

     try { 
      console.log("$$$$$$$$$$$$$$$ pushed : "+ rows1[0].num_count); 
      contents.push(rows1[0].num_count); 
      console.log('contents : '+ contents+'\n');       

     } catch (e) { 
      if (e instanceof SyntaxError) { 
       console.log("Syntax Error for input function"); 
      } 
     } 

    }); 

    console.log("##################### " + query + "\n"); 

    console.log('contents : '+ contents+'\n'); 
} 

任何意見,無論是在如何阻止直到的NodeJS從查詢結果中獲得或以其他方式重組我的代碼?

在此先感謝。

+0

關於這個話題有很多關於SO的問題,並且有很多可靠的解決方案。使用'block'或'synchronous'或'callback hell'等術語在'[node.js]'標籤中進行一些搜索:)。 – JohnnyHK 2013-03-12 04:08:37

回答

2

在執行之前,您沒有在等待您的查詢。您可以查看node-mysql-queues模塊,您可以使用該模塊排隊查詢,並在所有執行完畢後執行給定回調(它也將允許您執行交易)。您正在執行的查詢數量。在每個事務的回調中,將結果保存到您的返回對象中並遞減計數器。如果它是< = 0,那麼你的所有查詢都已經完成,你可以用返回對象執行主回調。

另外,請注意SQL注入。

+0

謝謝尼克,那樣做... – 2013-03-12 05:03:50

0

做下面的事情可能是一個解決辦法。

//它只是一個測試什麼來銘記第一,所以我會調整它後...

 var contents = []; 

     var lock = 0; 

     for (var i = step_min; i < (step_max + step); i += step) { 
      lock++; 
     } 

     for (var i = step_min; i < (step_max + step); i += step) { 
      query = 'select count(' + colName + ') as num_count from ' + rows[0].tablename + ' where ' + 'dictionaryid=' + rows[0].dictionaryid + ' and ' + colName + ' between ' + i + ' and ' + (i + step); 

      connection.query(query, function(err, rows1, fields) { 
       if (err) { 
        console.log(err); 
        throw err; 
       } 

       try { 
        console.log("$$$$$$$$$$$$$$$ pushed : " + rows1[0].num_count); 
        contents.push(rows1[0].num_count); 
        console.log('contents : ' + contents + '\n'); 

       } catch (e) { 
        if (e instanceof SyntaxError) { 
         console.log("Syntax Error for input function"); 
        } 
       } 

       lock--; 

       if (lock == 0) { 
        queryDone(); 
       } 

      }); 

     } 

     function queryDone() { 


      console.log("##################### " + query + "\n"); 

      console.log('contents : ' + contents + '\n'); 


      var id = obj.exptID + '.' + obj.exptITR + '.' + obj.nodeID + '.' + obj.resourceName + '.' + colName; 
      serie = { 
       name: colName, 
       data: contents, 
       id: id 
      }; 



      console.log("--------------------\n " + step_max + "\n" + step_min + "\n------------------------\n"); 
     } 

的方法是火的功能,當尼克說,所有查詢都做代碼.. 。

1

試試這個: https://github.com/luciotato/waitfor

你的代碼wait.for:

for (var i = step_min; i < (step_max); i += step) { 
    query = 'select count(' + colName + ') as num_count from ' + 
     rows[0].tablename + ' where ' + 'dictionaryid=' + 
     rows[0].dictionaryid + ' and ' + colName + ' between ' + i + 
     ' and ' + (i + step); 

    var rows1 = wait.forMethod(connection,"query",query); //waits until callback 
    console.log("$$$$$$$$$$$$$$$ pushed : "+ rows1[0].num_count); 
    contents.push(rows1[0].num_count); 
} 
console.log("##################### " + query + "\n"); 
console.log('contents : '+ contents+'\n'); 

} catch (e) { 
      if (e instanceof SyntaxError) { 
       console.log("Syntax Error for input function"); 
      } 
      .... 
     }