最新消息:20190717 VPS服务器:Vultr新加坡,WordPress主题:大前端D8,统一介绍入口:关于

【已解决】Node.js中测试mysql的代码var client = mysql.createClient运行出错:TypeError: Object # has no method ‘createClient’

Node.js crifan 2799浏览 0评论

【问题】

折腾:

【记录】折腾Node.js + mysql + restify

的过程中,在Node.js中安装了mysql:

CLi@PC-CLI-1 /cygdrive/d/tmp/tmp_dev_root/node.js
$ npm install mysql
npm http GET https://registry.npmjs.org/mysql
npm http 200 https://registry.npmjs.org/mysql
npm http GET https://registry.npmjs.org/mysql/-/mysql-2.0.0-alpha4.tgz
npm http 200 https://registry.npmjs.org/mysql/-/mysql-2.0.0-alpha4.tgz
npm http GET https://registry.npmjs.org/require-all/0.0.3
npm http 200 https://registry.npmjs.org/require-all/0.0.3
npm http GET https://registry.npmjs.org/require-all/-/require-all-0.0.3.tgz
npm http 200 https://registry.npmjs.org/require-all/-/require-all-0.0.3.tgz
mysql@2.0.0-alpha4 node_modules\mysql
└── require-all@0.0.3

之后,去测试mysql。

相关代码如下:

var mysql = require('mysql');  
  
var TEST_DATABASE = 'nodejs_db';  
var TEST_TABLE = 'test';  
  
//创建连接  
var client = mysql.createClient({  
  user: 'root',  
  password: 'rainbow',  
});
  
......

结果出错:

结果出错:

D:\tmp\tmp_dev_root\node.js\mysql\mysql_test.js:7
var client = mysql.createClient({
                   ^
TypeError: Object #<Object> has no method 'createClient'
    at Object.<anonymous> (D:\tmp\tmp_dev_root\node.js\mysql\mysql_test.js:7:20)
    at Module._compile (module.js:449:26)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.runMain (module.js:492:10)
    at process.startup.processNextTick.process._tickCallback (node.js:244:9)

 

【解决过程】

1.找了半天,终于找到了个有用的:

Not working with latest version of node-mysql

然后参考其解释:

v2.0.0-alpha (2012-05-15)

This release is a rewrite. You should carefully test your application after

upgrading to avoid problems.

The first thing you will run into is that the old Client class is gone and

has been replaced with a less ambitious Connection class. So instead of

mysql.createClient(), you now have to:

var mysql = require(‘mysql’);

var connection = mysql.createConnection({

host : ‘localhost’,

user : ‘me’,

password : ‘secret’,

});

把源码改为:

var mysql = require('mysql');

var TEST_DATABASE = 'nodejs_db';
var TEST_TABLE = 'test';

// //创建连接
// var client = mysql.createClient({
  // user: 'root',
  // password: 'rainbow',
// });
var connection = mysql.createConnection({
    host : 'localhost',
    user : 'root',
    password : 'xxxxxx',
});

//创建数据库
connection.query('CREATE DATABASE '+TEST_DATABASE, function(err) {
  if (err && err.number != mysql.ERROR_DB_CREATE_EXISTS) {
    throw err;
  }
});

//不指定回调函数,如果出错,则体现为客户端错误
connection.query('USE '+TEST_DATABASE);

//创建表格,插入数据
connection.query(
  'CREATE TABLE '+TEST_TABLE+
  '(id INT(11) AUTO_INCREMENT, '+
  'name VARCHAR(255), '+
  'PRIMARY KEY (id))'
);

connection.query(
  'INSERT INTO '+TEST_TABLE+' '+
  'SET name = ?',
  ['nodejs1']
);

var query = connection.query(
  'INSERT INTO '+TEST_TABLE+' '+
  'SET name = ?',
  ['nodejs2']
);

//查询,并设置回调函数
connection.query(
  'SELECT * FROM '+TEST_TABLE,
  function selectCb(err, results, fields) {
    if (err) {
      throw err;
    }

    console.log(results);
    console.log(fields);
    connection.end();
  }
);

然后就可以成功运行了:

D:\tmp\tmp_dev_root\node.js\mysql>node mysql_test.js
[ { id: 1, name: 'nodejs1' }, { id: 2, name: 'nodejs2' } ]
[ { catalog: 'def',
    db: 'nodejs_db',
    table: 'test',
    orgTable: 'test',
    name: 'id',
    orgName: 'id',
    filler1: <Buffer 0c>,
    charsetNr: 63,
    length: undefined,
    type: 3,
    flags: 16899,
    decimals: 0,
    filler2: <Buffer 00 00>,
    default: undefined,
    zeroFill: false,
    fieldLength: 11 },
  { catalog: 'def',
    db: 'nodejs_db',
    table: 'test',
    orgTable: 'test',
    name: 'name',
    orgName: 'name',
    filler1: <Buffer 0c>,
    charsetNr: 33,
    length: undefined,
    type: 253,
    flags: 0,
    decimals: 0,
    filler2: <Buffer 00 00>,
    default: undefined,
    zeroFill: false,
    fieldLength: 765 } ]

然后也可以通过MySQL的命令行(通过开始菜单找到MySQL -> MySQL 5.5 Command Line Client),验证出结果的确新增了对应的nodejs_db:

Enter password: ********
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.5.18 MySQL Community Server (GPL)

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| joomla             |
| mysql              |
| performance_schema |
| test               |
| wordpress          |
+--------------------+
6 rows in set (0.08 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| joomla             |
| mysql              |
| nodejs_db          |
| performance_schema |
| test               |
| wordpress          |
+--------------------+
7 rows in set (0.02 sec)

mysql>

 

【总结】

运行mysql.createClient出错的原因在于,新版的Node.js中的mysql,已经去掉了createClient,而改为了mysql.createConnection。

所以换为mysql.createConnection,就可以正常运行了。

转载请注明:在路上 » 【已解决】Node.js中测试mysql的代码var client = mysql.createClient运行出错:TypeError: Object # has no method ‘createClient’

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

网友最新评论 (2)

  1. 对我这样的小白很有帮助,赞。网上的都是以前的写法,结果总是报错。
    朗郎5年前 (2015-03-16)回复
  2. 帮了大忙呐, 找到的好些代码都是老的范例, 运动都报错 我就想是不是最近更新了mysql这模块的, 刚好就找到你这篇文章 现在版本是mysql@2.0.0-alpha5 了
    demen7年前 (2013-01-28)回复
63 queries in 0.130 seconds, using 18.91MB memory