【问题标题】:object is not a function in mysql db using with nodejs对象不是与 nodejs 一起使用的 mysql db 中的函数
【发布时间】:2012-06-17 08:28:48
【问题描述】:

我正在关注tutorial,但首先我收到了这个错误:

deprecated: connect() is now done automatically

我更改了我的代码,但之后我遇到了这个错误:

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');
};

【问题讨论】:

    标签: mysql node.js


    【解决方案1】:

    不确定这是导致您的特定消息的原因,但在ClientReady 中,您在INSERT 有机会执行之前调用了GetData;您应该在来自INSERT 的回调内部进行。

    更新:现在有人很好地正确缩进了所有内容(提示:应该缩进的东西应该开始以4个空格)很明显你在GetData()中遇到了类似的问题:在您的回调有机会执行之前,您正在调用client.end()。因此,您的 SELECT 回调正在尝试使用关闭的 MySQL 连接,这可能导致失败。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-08
      • 1970-01-01
      • 2014-01-12
      • 1970-01-01
      • 2011-08-18
      • 2022-11-14
      • 2015-01-11
      相关资源
      最近更新 更多