Async.js 系列和节点-mysql 查询无法获取行

Async.js series and node-mysql query's cant get rows

我目前正在尝试 运行 一组 MySQL 查询,以便使用 async.js 系列控制流函数。但我不断收到以下错误:

throw err; // Rethrow non-MySQL errors
        ^

TypeError: Cannot read property 'status' of undefined

我已经在 async.series 之外的单独函数中测试了查询,它们都很好,并把数据还给我,我能想到的错误的唯一原因是由于它不是异步性质当时有数据,因此当我记录我得到的行时出现错误 E.G:

[]
[]
[]

下面是异步函数:

 function SQLuserDataAsync() {
    connection.getConnection(function (err, connection) {
        async.series([
                function (callback) {
                    connection.query('SELECT status FROM users WHERE name= ?;',
                        [userval],
                        function (err, rows) {
                        if (rows[0]['status']) {
                            console.log("Account Status: " + accountval);
                        } else {
                            console.log(err);
                        }
                        callback(null, 'one');
                    });
                },
                function (callback) {
                    connection.query('SELECT account_type FROM settings_tbl WHERE id=(SELECT id FROM users WHERE name= ?);',
                        [userval],
                        function (err, rows) {
                        if (rows[0]['account_type']) {
                            var acctype = rows[0]['account_type'];
                            console.log("Account Type: " + acctype);
                        } else {
                            console.log(err);
                        }
                        callback(null, 'two');
                    });
                },
                function (callback) {
                    connection.query('SELECT type FROM settings_tbl WHERE id=(SELECT id FROM users WHERE name= ?);',
                        [userval],
                        function (err, rows) {
                        if (rows[0]['type']) {
                            var type = rows[0]['type'];
                            console.log("Type: " + type);
                        } else {
                            console.log(err);
                        }
                        callback(null, 'three');
                    });
                }
            ]);
        connection.release();
    });
}

关于错误的原因或这里做错了什么有什么建议吗?

您错过了 async.series 函数的主要回调函数。

function SQLuserDataAsync() {
    connection.getConnection(function (err, connection) {
        async.series([
                function (callback) {
                    // YOUR CODE
                },
                function (callback) {
                    // YOUR CODE
                },
                function (callback) {
                    // YOUR CODE
                }
            ], function(error, results) { // <--- this is the main callback
                connection.release();
            });
    });
}

您应该在主回调中调用 connection.release(),否则,MySQL 连接将在执行查询之前 released/terminated(由于代码的异步性质)。

如果在 userval 名称中定义了一个用户,它将起作用。

但是让我们简化代码:

function SQLuserDataAsync(userval) {

    connection.getConnection(function (err, connection) {
        async.waterfall([

                // getting user
                function (next) {
                    connection.query(
                        'SELECT * FROM users WHERE name = ? LIMIT 1',
                        [userval],
                        function (err, result) {
                          next(err, result[0]); // passing user to next function
                        });
                },

                // getting settings of user, maybe user_id (not id) in query below
                function (user, next) {
                    connection.query(
                        'SELECT * FROM settings_tbl WHERE id = ? LIMIT 1',
                        [user.id],
                        function (err, result) {
                          next(err, user, result[0]);
                        });
                },

                // handling both user and settings
                function (user, settings, next) {
                  console.log('User: ', user);
                  console.log('Settings: ', settings);
                  connection.release();
                }
            ]);
    });
}

SQLuserDataAsync('someone');