node连接mysql出错
发布于 2年前 作者 zhangking 1357 次浏览

我的代码 var Client = require(‘mysql’).Client; var client = new Client(); client.host = 'localhost’; client.port = 3306; client.user = 'root’; client.password = '123456’; client.database=’test1’;

query(client);

function query(client){ client.query( 'select * from user’, function(err,res,fields){ console.log(res); client.end(); } ); }; 报错 /home/king/node/mysql.js:2 var client = new Client(); ^ TypeError: undefined is not a function at Object.<anonymous> (/home/king/node/mysql.js:2:14) at Module._compile (module.js:456:26) at Object.Module._extensions…js (module.js:474:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:312:12) at Function.Module.runMain (module.js:497:10) at startup (node.js:119:16) at node.js:901:3 这是mysql 模块没加载么,可是我的目录下已经有啦含mysql的node_moudle啦,求解惑

2 回复

改成这样试试

var Client = require('mysql').createConnection({

        host:Db.host,

        user:Db.user,

        password:Db.password,

        database: Db.database

    });

可以啦,谢谢,这个方法是直接返回了一个实例化对象么

回到顶部