Search code examples
javascriptnode.jsasynchronousnode-mysqlasync.js

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


I am currently trying to run a set of MySQL query's in order using async.js series control flow function. But I keep receiving the following error:

throw err; // Rethrow non-MySQL errors
        ^

TypeError: Cannot read property 'status' of undefined

I have tested the query's in seperate functions outside the async.series and they are fine and give me back the data, the only reason I can think for the error is due to the async nature it doesn't have the data at that time hence the error E.G when I log the rows I get:

[]
[]
[]

Below is the Async function:

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

Any suggestions as the reason for the error or what am doing wrong here?


Solution

  • You've missed the main callback function to the async.series function.

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

    You should call connection.release() inside the main callback, otherwise, the MySQL connection will be released/terminated before the queries are executed (due to the asynchronous nature the code).