适用于Node.js的MySQL库中的错误 [英] Error in MySQL library for Node.js

查看:102
本文介绍了适用于Node.js的MySQL库中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Node.js应用程序中,我试图连接到Amazon上托管的MySQL数据库.

In my Node.js app, I am trying to connect to a MySQL database hosted on Amazon.

$ npm install mysql

我的代码如下:

var mysql      = require('mysql');
var connection = mysql.createConnection({
  host     : 'my amazon sql db',
  user     : 'me',
  password : 'secret',
  database : 'my_db'
});

connection.connect();

connection.query('SELECT 1 + 1 AS solution', function(err, rows, fields) {
  if (err) throw err;

  console.log('The solution is: ', rows[0].solution);
});

connection.end();

我可以使用Workbench连接到我的MySQL数据库-因此,我很确定我的凭据可以.

I can connect to my MySQL DB using Workbench--therefore, I am pretty sure my credentials are okay.

当我尝试连接时,出现以下错误:

When I attempt to connect I get the following error:

Connection.js:91未捕获的TypeError:Net.createConnection不是函数

Connection.js:91 Uncaught TypeError: Net.createConnection is not a function

从npm库中调试代码-这是在connection.js中引发错误的地方:

Debugging the code from the npm library--this is where the error is thrown in connection.js:

this._socket = (this.config.socketPath)
  ? Net.createConnection(this.config.socketPath)
  : Net.createConnection(this.config.port, this.config.host);

connection.js具有依赖项:

var Net  = require('net');

我正在Windows计算机上本地运行Node.js.

I am running Node.js locally on my Windows computer.

谁能告诉我是什么原因导致此错误?

Can anyone tell me what could be causing this error?

创建了一个单独的票证: 在调用Node.js net.createConnection时抛出错误

Created a separate ticket: Error thrown calling Node.js net.createConnection

推荐答案

MySQL节点模块中必需和使用的net模块是Node.js本身的核心部分.您遇到的不是Net.createConnection函数的错误意味着它正在作为一个空对象出现,并且该错误与您对问题的评论之一有关:

The net module required and used in the MySQL node module is a core part of Node.js itself. The error you're getting about Net.createConnection not being a function means it's coming up as an empty object and the error is related to one of your comment to the question:

我正在浏览器中测试我的代码.

I am testing my code within a browser.

您只能在Node.js上运行此特定模块,而不能在Web浏览器中运行它.

You must run this particular module on Node.js only, you can't run it in a web browser.

有人认为可能会通过 browserify

One could think a possibility would be to run your code through a packer like browserify or webpack so you can easily require('mysql') in your browser but it won't work. The net module which is a core dependency of the mysql module will be transformed into an empty object {}. That's not a bug, it's how it's supposed to work. Browsers don't have generic tcp implementations so it can't be emulated. The empty object is intended to prevent require('net') from failing on modules that otherwise work in the browser.

为避免此错误,您需要在纯Node.js环境中而不是在浏览器中运行此代码.一个简单的服务器可以达到这个目的,因为浏览器中客户端中的此代码无法正常工作,并且会增加安全漏洞,因为客户端的所有操作都是操作性的,因此也不安全.您不想在客户端公开数据库,而只使用数据库.

To avoid this error, you need to run this code in a pure Node.js environment, not in a browser. A simple server could serve this purpose since this code in your client in a browser can't work and would add a security hole as everything client-side is manipulative and as such not secure. You don't want to expose your database on the client-side but only consumes it.

这篇关于适用于Node.js的MySQL库中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆