I have this function that I need to return true or false. Problem is, it doesn't seem to behave correctly. If it does return false, it doesn't disconnect the client, and if it does return true, it still does "console.log('Disconnecting client...')" I am at a complete lost. :( This is for node.js mysql and socket.io
CODE:
if(!client_to_server_00(header,data,len)){
// should send the server a packet to terminate the connection on
their end...
// It should send 0x00 SERVER->CLIENT (will implement later)
console.log('Disconnecting client...'); // This always shows no matter what
client.emit('disconnect'); // **This does not work!
}else{
console.log('ID VERIFIED! WELCOME!');
}
==================================================================================
// LOGIN INFORMATION
function client_to_server_00(header, data, len){
// We are already connected to the database
var z = decodeFromHex(data); // find the username
z = returnvalue(z,1,len); // the code was a little off (ill fix it later)
console.log(z); // make sure we have this correct
// In this example. The database returns 'oh' And z = 'oh'
client.query('USE '+TEST_DATABASE);
client.query(
'SELECT * FROM '+TEST_TABLE,
function selectCb(err, results, fields) {
if (err) {
throw err;
}
var first = results[0];
console.log('USRNAME: ['+first['name']+']'); // Make sure we have it
if(first['name'] == z){
console.log('Correct');
return true; // I need client_to_server_00 function to return true if correct
}else{
console.log('Incorrect');
return false; // I need client_to_server_00 function to return false if incorrect
}
})
};
It's very common mistake. You are missing very basic concept of async programming - you have to pass callback to your client_to_server_00
function and call it when you're done with async stuff (database connection in this case).