Search code examples
node.jsnode-mysql

How to access to the value when doing a COUNT() using node-mysql?


I am trying to do a count() in node.js using the mysql module but I am not able to access to the value since its index is count(user):

(result of printing the variable result)

My code:

function registerUser(user, password) {
    return new Promise(function(resolve, reject) {
        const sql1 = "SELECT COUNT(user) FROM users WHERE user = '" + user + "'";
        con.query(sql1, function (err, result) {
            console.log(result[0].COUNT(user)); //here it should print out 13
        });     
    });
}

I get this error instead:

I assume that COUNT() is taking it as a function, not as an index.

Does anyone know how to achieve that?


Solution

  • Add square brackets around the 'COUNT(user)' string:

    console.log(result[0]['COUNT(user)']);