这可能真的很明显。。。。我需要在函数中做什么来确保这个过程继续进行?
function processRow(row){
console.log(row.title);
query.resume();
}
console.log("connecting to MySQL")
var connection = Mysql.createConnection({
host: '178.62.123.210',
user: 'mongo',
password: 'xxxxx',
database: 'd14'
});
connection.connect(function (err) {
if (err) {
console.log('error connecting: ' + err.stack);
}
console.log('connected as id ' + connection.threadId);
});
var query = connection.query('SELECT * from archives limit 50');
query.on('result', function(row) {
// Pausing the connnection is useful if your processing involves I/O
connection.pause();
processRow(row, function() {
connection.resume();
});
});
它做一个记录并停止(还有更多)
您正在调用query.resume()
。它应该是connection.resume()
(您在其他地方调用它)。选择您希望恢复工作的地点,然后在那里致电connection.resume()
。
我将更仔细地查看node-mysql页面上列出的示例。代码中有几个错误,与示例显示的不同,例如query.resume()
和在connection.connect
回调中遇到错误时不返回。您的代码还将回调传递给processRow
,而processRow
从不调用该回调。
以下是一个修复了更明显问题的版本:
// I would move this down nearer to where it's used
function processRow(row, callback) { // <=== accept the callback
console.log(row.title);
callback(); // <=== call the callback
}
console.log("connecting to MySQL")
var connection = Mysql.createConnection({
host: '178.62.123.210',
user: 'mongo',
password: 'xxxxx',
database: 'd14'
});
connection.connect(function (err) {
if (err) {
console.log('error connecting: ' + err.stack);
return; // <=== return, don't fall through
}
console.log('connected as id ' + connection.threadId);
});
// No need for a query variable here
connection.query('SELECT * from archives limit 50')
.on('result', function(row) {
connection.pause();
processRow(row, function() {
connection.resume();
});
});
好的,它确实需要connection.resume();我缺乏知识,不明白为什么需要两个。
function processRow(row){
console.log(row.title);
connection.resume();
}
console.log("connecting to MySQL")
var connection = Mysql.createConnection({
host: '178.62.123.210',
user: 'mongo',
password: 'xxx',
database: 'd14'
});
connection.connect(function (err) {
if (err) {
console.log('error connecting: ' + err.stack);
}
console.log('connected as id ' + connection.threadId);
});
var query = connection.query('SELECT * from archives limit 50');
query.on('result', function(row) {
// Pausing the connnection is useful if your processing involves I/O
connection.pause();
processRow(row, function() {
connection.resume();
});
});