3

I've written a code that should retrieve data from database with nodejs and mysql but it's not working.

It says the I didn't installed the mysql module but I did, using npm install: enter image description here

The proof that I installed the mysql module successfully: enter image description here

Is my code ok ? Keep in mind that I'm completely new to node.js.

var http = require('http');

http.createServer(function(request, response){
	response.writeHead(200, {'Content-Type': 'text/plain'});
	response.write('My first node server...');
		var mysql = require('mysql');
			var connection = mysql.createConnection({
				host :  'localhost:8888',
				user : 'root',
				password: '',
				database: 'test'
			});

			connection.connect();

			connection.query('SELECT * FROM tabel_test', function(err, rows, fields){
					if (!err) {
						console.log('The result is ', rows);
					} else {
						console.log('No results.');
					}
			});

		connection.end();
	response.end();
}).listen(8888);

15
  • What exactly does "it's not working" mean? Commented Oct 1, 2015 at 14:07
  • Can't establish a connection to the server when I use mysql. I use the command terminal with 'node database_nodejs.js' command which is the name of the file where I have the code from my question. Commented Oct 1, 2015 at 14:08
  • Do you can show the output console error? Commented Oct 1, 2015 at 14:11
  • 2
    Personally I found Sequelize to be more useful than the plain mysql library. Commented Oct 1, 2015 at 14:26
  • 1
    I know it doesn't help; that's why I wrote it as a comment not an answer. As for more useful ... well, it's an ORM, so you get to use models and such; it abstracts your code away from the DB itself. It also supports multiple databases, which is helpful if you ever wanted to switch to postrgreSQL, for example. Commented Oct 1, 2015 at 14:34

2 Answers 2

2

The code seems fine, the exception says that you do not have the mysql module, that could be technically true.

Make sure you have the node_modules\mysql folder in the current working directory, or that you have installed it globally with npm install -g mysql.

By the way I suggested the package.json file, because in there you could define the required version of the mysql module. Then if you do npm intall in the folder, the enumerated dependencies will be downloaded to the current working directory. This also make it easier for others to use you code, if you put it into a git repository.

Sign up to request clarification or add additional context in comments.

8 Comments

Updated my question. Added the screenshot with the install of mysql module too.
Well, it may be built there, but that does not mean, that is available for your app. Use the global install command, and show us the output. If you have a package.json, that would be great too.
What does this mean: "If you have a package.json, that would be great too." How will this help me? What should I do?
@lonut Does npm ls run in your project's root shows the mysql package? If not then in the end you need to install it like @meskobalazs said. I personally prefer not to install packages globally, but I am no expert.
Yes, because the module is named mysql, not node-mysql-master. It seems to my you are using a git version for some reason, why?
|
0

if you found the given path node/node_modules/mysql/lib change the below line connection.query('SELECT * FROM test_user', function(err, rows, fields) connection.end();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.