什么阻止了这个查询的其余部分?

时间:2015-06-26 17:19:32

标签: javascript mysql node.js meteor

这可能非常明显......我需要在函数中做些什么来确保这个过程继续进行?

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

它会记录并停止(还有更多)

2 个答案:

答案 0 :(得分:0)

您正在致电query.resume()。它应该是connection.resume()(你在其他地方打电话)。选择您想要恢复的位置,然后在那里拨打connection.resume()

我会更仔细地看一下例子listed on the node-mysql page。您的代码中存在多个错误,这些错误与示例显示的内容不同,例如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();
        });
    });

答案 1 :(得分:-1)

好的,它确实需要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();
    });
});