2012-04-12 48 views
0

我有这个函数,我需要返回true或false。问题是,它似乎不正确。如果它返回false,它不会断开客户端,如果它返回true,它仍然会“console.log('Disconnecting client ...')”我完全失去了。 :(这是node.js的MySQL和socket.ioJS node-mysql布尔表现怪异

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 
} 

}) 
}; 

回答

1

这是非常常见的错误,这是因为丢失异步编程的很基本的概念 - 你必须通过回调到您的client_to_server_00函数并调用它,当你与异步的东西做(在这种情况下数据库连接)