我对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