Rejaul
Rejaul

Reputation: 980

node.js mysql client undefined

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/

Upvotes: 0

Views: 1814

Answers (2)

Abhishek Dhanraj Shahdeo
Abhishek Dhanraj Shahdeo

Reputation: 1356

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...!

Upvotes: 0

Rodrigo Medeiros
Rodrigo Medeiros

Reputation: 7862

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.

Upvotes: 2

Related Questions