2012-04-12 53 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函數並調用它,當你與異步的東西做(在這種情況下數據庫連接)