Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
425 views
in Technique[技术] by (71.8m points)

Error in MySQL library for Node.js

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

$ npm install mysql

My code looks something like this:

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();

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 Uncaught TypeError: Net.createConnection is not a function

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

The connection.js has a dependency :

var Net  = require('net');

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

Can anyone tell me what could be causing this error?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...