我對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從查詢結果中獲得或以其他方式重組我的代碼?
在此先感謝。
關於這個話題有很多關於SO的問題,並且有很多可靠的解決方案。使用'block'或'synchronous'或'callback hell'等術語在'[node.js]'標籤中進行一些搜索:)。 – JohnnyHK 2013-03-12 04:08:37