Node.js returning result from MySQL query

You have to do the processing on the results from the db query on a callback only. Just like.

function getColour(username, roomCount, callback)
    connection.query('SELECT hexcode FROM colours WHERE precedence = ?', [roomCount], function(err, result)
        if (err) 



//call Fn for db query with callback
getColour("yourname",4, function(err,data){
        if (err) {
            // error handling code goes here
            console.log("ERROR : ",err);            
        } else {            
            // code to execute on data retrieval
            console.log("result from db is : ",data);   


Leave a Comment