2012-06-17 29 views
1

我正在關注的tutorial但首先使用我得到這個錯誤:對象不是MySQL中的功能與數據庫的NodeJS

deprecated: connect() is now done automatically 

我改變了我的代碼,但是,即時通訊faceing此錯誤後:

object is not a function at Client.CALL_NON_FUNCTION_AS_CONSTRUCTOR (native) 

我不知道,因爲我是新來的nodejs和MySQL,所以好心幫助我!

這裏是我的代碼:

var Client = require('mysql').createClient({'host':'localhost','port':'3306','user':'root', 'password':''}); 
var client = new Client(); 

console.log('Connecting to mysql..'); 
ClientConnectionReady(client); 

ClientConnectionReady = function(client){ 
    client.query('USE login',function(err,result){ 
    if(err){ 
      console.log('cant create Table Error:'+ err.message); 
     client.end(); 
     return; 
} 
ClientReady(client); 
}; 

ClientReady = function(client){ 
    var values = ['fahad','tariq']; 
    client.query('INSERT into Login SET username = ?, password = ?',values, 
    function(err,result){ 
     if(err){ 
      console.log("ClientReady Error:" + error.message); 
      client.end(); 
      return; 
     } 
     console.log('inserted:'+result.affectedRows+'ros.'); 
     console.log('Id inserted:'+ result.insertId); 
    }); 
    GetData(client); 
} 

GetData = function(client){ 
    client.query(
     'SELECT * FROM Login', 
      function selectCb(err,result,fields){ 
       if(err) { 
        console.log('Getdata Error'+err.message); 
        client.end(); 
        return; 
       } 
       if(result.length>0) { 
        var firstResult = results[0]; 
        console.log('username' + firstResult['username']); 
        console.log('password'+firstResult['password']); 
       } 
      }); 
    client.end(); 
    console.log('connection closed'); 
}; 

回答

0

不知道這是什麼導致你的具體消息,但在ClientReady你打電話GetDataINSERT面前有機會執行;你應該這樣做裏面INSERT回調。

更新:現在,有人還跟正確縮進一切(提示:東西,應縮進應該開始有4位),可以清楚地看到你在GetData()一個類似的問題:你打電話之前client.end()你的回調有機會執行。因此你的SELECT回調試圖使用一個關閉的MySQL連接,這可能導致失敗。

相關問題