适用于Node.js的MySQL库中的错误

编程入门 行业动态 更新时间:2024-10-25 08:25:44
本文介绍了适用于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 或 webpack ,因此您可以轻松地在浏览器中require('mysql'),但它将无法正常工作.作为mysql模块核心依赖项的net模块将转换为空对象{}. 那不是一个bug,而是它应该如何工作的.浏览器没有通用的tcp实现,因此无法对其进行仿真.空对象旨在防止require('net')在浏览器中正常运行的模块上失败.

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库中的错误

本文发布于:2023-11-28 19:44:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1643714.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:适用于   库中   错误   js   Node

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!