Domanda

I used queryAsync() as prescribed by an answer to a question, and it works, but it adds lots of extra meta data to my query result, which is otherwise a straightforward query.


Here's what I'm using to process/execute the query and log the result:

var retrieveSettings = Promise.method(function (username, connection) {
    console.log('User ' + username + ' retrieving settings...');
    var q = 'select * from sales_settings';
    return connection.queryAsync(q).then(function (rows, fields) {
        list = [];
        for (x = 0; x < rows.length; x++) {
            list.push(rows[x]);
        }
        //console.log('Settings: ' + JSON.stringify(list, null, 4));
        return list;
    });
});

and here is the result that is logged:

Settings: [
    [
        {
            "group_name": "add_auto",
            "commission_rate": 0,
            "monthly_req": 0,
            "category_name": "Auto",
            "commission_type": "none",
            "group_title": "Added Auto"
        },
        {
            "group_name": "add_fire",
            "commission_rate": 0,
            "monthly_req": 0,
            "category_name": "Fire",
            "commission_type": "none",
            "group_title": "Added Fire"
        },
        {
            "group_name": "bank_dep",
            "commission_rate": 25,
            "monthly_req": 0,
            "category_name": "Bank",
            "commission_type": "static",
            "group_title": "Bank Deposit"
        },
        {
            "group_name": "bank_loan",
            "commission_rate": 75,
            "monthly_req": 8,
            "category_name": "Bank",
            "commission_type": "static",
            "group_title": "Bank Loan"
        },
        {
            "group_name": "health",
            "commission_rate": 0.084,
            "monthly_req": 4,
            "category_name": "Health",
            "commission_type": "premium",
            "group_title": "Health App"
        },
        {
            "group_name": "life",
            "commission_rate": 0.084,
            "monthly_req": 8,
            "category_name": "Life",
            "commission_type": "premium",
            "group_title": "Life App"
        },
        {
            "group_name": "new_auto",
            "commission_rate": 0.03,
            "monthly_req": 32,
            "category_name": "Auto",
            "commission_type": "rate",
            "group_title": "Raw New Auto"
        },
        {
            "group_name": "new_fire",
            "commission_rate": 0.03,
            "monthly_req": 20,
            "category_name": "Fire",
            "commission_type": "rate",
            "group_title": "Raw New Fire"
        }
    ],
    [
        {
            "catalog": "def",
            "db": "officeball",
            "table": "sales_settings",
            "orgTable": "sales_settings",
            "name": "group_name",
            "orgName": "group_name",
            "filler1": [
                12
            ],
            "charsetNr": 33,
            "length": 135,
            "type": 253,
            "flags": 20483,
            "decimals": 0,
            "filler2": [
                0,
                0
            ],
            "zeroFill": false,
            "protocol41": true
        },
        {
            "catalog": "def",
            "db": "officeball",
            "table": "sales_settings",
            "orgTable": "sales_settings",
            "name": "commission_rate",
            "orgName": "commission_rate",
            "filler1": [
                12
            ],
            "charsetNr": 63,
            "length": 13,
            "type": 246,
            "flags": 4097,
            "decimals": 3,
            "filler2": [
                0,
                0
            ],
            "zeroFill": false,
            "protocol41": true
        },
        {
            "catalog": "def",
            "db": "officeball",
            "table": "sales_settings",
            "orgTable": "sales_settings",
            "name": "monthly_req",
            "orgName": "monthly_req",
            "filler1": [
                12
            ],
            "charsetNr": 63,
            "length": 11,
            "type": 3,
            "flags": 4097,
            "decimals": 0,
            "filler2": [
                0,
                0
            ],
            "zeroFill": false,
            "protocol41": true
        },
        {
            "catalog": "def",
            "db": "officeball",
            "table": "sales_settings",
            "orgTable": "sales_settings",
            "name": "category_name",
            "orgName": "category_name",
            "filler1": [
                12
            ],
            "charsetNr": 33,
            "length": 135,
            "type": 253,
            "flags": 4097,
            "decimals": 0,
            "filler2": [
                0,
                0
            ],
            "zeroFill": false,
            "protocol41": true
        },
        {
            "catalog": "def",
            "db": "officeball",
            "table": "sales_settings",
            "orgTable": "sales_settings",
            "name": "commission_type",
            "orgName": "commission_type",
            "filler1": [
                12
            ],
            "charsetNr": 33,
            "length": 135,
            "type": 253,
            "flags": 4097,
            "decimals": 0,
            "filler2": [
                0,
                0
            ],
            "zeroFill": false,
            "protocol41": true
        },
        {
            "catalog": "def",
            "db": "officeball",
            "table": "sales_settings",
            "orgTable": "sales_settings",
            "name": "group_title",
            "orgName": "group_title",
            "filler1": [
                12
            ],
            "charsetNr": 33,
            "length": 72,
            "type": 253,
            "flags": 4097,
            "decimals": 0,
            "filler2": [
                0,
                0
            ],
            "zeroFill": false,
            "protocol41": true
        }
    ]
]

Why is this unusual meta data being added to my query result?

È stato utile?

Soluzione 2

The standard callback from mysql's query() returns both a rows and fields. The asynch wrapper you are using (for example if you Q.denodeify()) is only returning a single thing, so for callbacks with multiple parameters it returns an array of [param1,param2] in this case [rows,fields].

Related to this, your function inside the then needs to accept only one parameter (which would be this array of [rows,fields]).

See the second portion of my answer to this question as an illustration.

Your code should thus be something like:

var retrieveSettings = Promise.method(function (username, connection) {
    console.log('User ' + username + ' retrieving settings...');
    var q = 'select * from sales_settings';
    return connection.queryAsync(q).then(function (results) {
        var rows = results[0]; // get the rows
        var fields = results[1]; // don't really care, but just for illustration
        list = [];
        for (x = 0; x < rows.length; x++) {
            list.push(rows[x]);
        }
        //console.log('Settings: ' + JSON.stringify(list, null, 4));
        return list;
    });
});

And actually, for the above code, there's no reason your entire callback can't just be:

    return connection.queryAsync(q).then(function (results) {
        return results[0]; // get the rows
    });

Altri suggerimenti

It looks like you are using bluebird, in that case you can use .spread:

var retrieveSettings = Promise.method(function (username, connection) {
    console.log('User ' + username + ' retrieving settings...');
    var q = 'select * from sales_settings';
    return connection.queryAsync(q).spread(function (rows, fields) {
        list = [];
        for (x = 0; x < rows.length; x++) {
            list.push(rows[x]);
        }
        //console.log('Settings: ' + JSON.stringify(list, null, 4));
        return list;
    });
});

The problem with the mysql module is that it doesn't conform to the node js callback standard which is (err, result). Instead it uses (err, result1, result2). Because functions can only throw one exception or return one value, bluebird returns an array of [result1, result2] to avoid information loss.

.spread is like .then except it assumes the fulfulliment value is an array and spreads the array's values over the arguments.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top