0

我有这个函数,我需要返回 true 或 false。问题是,它的行为似乎不正确。如果它确实返回 false,它不会断开客户端,如果它返回 true,它仍然会执行“console.log('Disconnecting client...')” 我完全迷失了。:(这是针对node.js mysql和socket.io的

代码:

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
}

})
};
4

1 回答 1

1

这是很常见的错误。您缺少异步编程的非常基本的概念 - 您必须将回调传递给您的client_to_server_00函数并在完成异步内容(在这种情况下为数据库连接)时调用它。

于 2012-04-12T15:19:22.363 回答