0

I'm trying to create a mysql database to node.js server. I've installed mysql module through command prompt:

npm install mysql

Then I execute the following code:

var Client = require('mysql').Client;
console.log(Client);

Console display undefined. That is, Client is undefined. Please tell me why it is undefined?

I'm following this tutorial http://utahjs.com/2010/09/22/nodejs-and-mysql-introduction/

7
  • Are you running node in the same directory your did npm install in? Commented Jun 30, 2014 at 17:17
  • Actually When run var Client = require('mysql').Client; console.log(Client); console display undefined Commented Jun 30, 2014 at 17:22
  • @user3782480 So it would be nice to update your question to reflect that. Commented Jun 30, 2014 at 17:30
  • 1
    I noticed when I run var Client = require('mysql');console.log(Client); Console displays all method inside Client variable but not Client method. Commented Jun 30, 2014 at 17:34
  • @user3782480 That information is useful too. You should put it in your question. And that's why I told in my answer below that the tutorial you're following is a bit old. Commented Jun 30, 2014 at 17:43

2 Answers 2

2

Maybe the the tutorial is a little bit old. Just use the instruction on the node-mysql docs:

var mysql = require('mysql');
var connection = mysql.createConnection({
  host     : 'localhost',
  user     : 'me',
  password : 'secret'
});

connection.connect();

And you should be able to connect to your MySQL database.

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

Comments

0

The node js APIs having been changing updating a lot in recent past, so it is highly possible that the tutorial you have been following is out of date according to the version you are using. You can follow the code example here I am updating or you may refer to something else, the only part that matters is it should work at minimum cost.

    var mysql = require('mysql');

    app.use( connection(mysql, {
        host: 'myhost',
        user: 'user_name',
        password: 'password',
        port: 3306,          //port mysql
        database: 'database_name',
        multipleStatements: 'true'  //false by default

    }, 'pool'));

    req.getConnection(function(err, connection) {
        connection.query("SELECT * FROM `table_name`;",function (error,row){
            if(!error){
                           //do something.....
            }
            else console.log("Error : "+err);
            });
        //do something else...
    });

Thank you...!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.