0

I have tried with Joel solution but getting some error. Can anybody tell me where is the problem.

app.js

var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs')
, mysql = require('mysql')
var client = mysql.createConnection({
 host: 'localhost',
 user: 'root',
 password: '',
});
client.connect();
app.listen(3232);
function handler (req, res) {
 fs.readFile(__dirname + '/index.html',
 function (err, data) {
 if (err) {
 res.writeHead(500);
 return res.end('Error loading index.html');
 }
 res.writeHead(200);
 res.end(data);
 });
}
io.sockets.on('connection', function (socket) {
 socket.on('login', function(usr, pass){
 var TEST_DATABASE = 'mysqltest';
 var TEST_TABLE = 'users';
 client.query('USE '+TEST_DATABASE);
 client.query('SELECT name FROM '+TEST_TABLE+' WHERE user = "'+usr+'" AND password = "'+pass+'"', function(err, results) {
 if (err) throw err;
 console.log(results[0]); // [{1: 1}]
 socket.emit('retuLogIn',results[0]['name']);
 });
 });
 socket.on('disconnect', function(){
 console.log('Server has disconnected');
 });
});

index.html

<html>
 <title>WebSocket Client Demo [socket.io]</title>
 <script src="http://localhost:3232/socket.io/socket.io.js"></script>
 <script>
 function connect() {
 try
 {
 var socket = io.connect('http://localhost:3232/');
 socket.on("connect",function(){
 document.getElementById('status').innerHTML ="Browser has connected to the app server";
 socket.emit('login', document.getElementById('txtUser').value,
 document.getElementById('txtPass').value);
 });
 socket.on('retuLogIn', function (data) {
 document.getElementById('status').innerHTML = 'Welcome '+data;
 });
 }
 catch(err)
 {
 document.getElementById('status').innerHTML = err.message;
 }
 }
 </script>
 <body>
 <h1>WebSocket Client Demo</h1>
 <div><p id="status">Enter user and password to Log-In</p></div>
 <label>User :</label>
 <input id="txtUser" type="text" maxlength="10" />
 <label>Password :</label>
 <input id="txtPass" type="text" maxlength="10" />
 <button id="connect" onClick='connect()'/>Log-In</button>
 </body>
</html>

When i try to run the app.js file within node.js it will give me the following error:

C:\Program Files (x86)\nodejs>node "C:\Program Files (x86)\nodejs\app.js" info - socket.io started
C:\Program Files (x86)\nodejs\app.js:6 var client = mysql.createConnection({ ^ TypeError: Object # has no method 'createConnection' at Object. (C:\Program Files (x86)\nodejs\app.js:6:24) at Module._compile (module.js:449:26) at Object..js (module.js:467:10) at Module.load (module.js:356:32) at Function._load (module.js:312:12) at module.js:487:10 at EventEmitter._tickCallback (node.js:238:9)
C:\Program Files (x86)\nodejs>

I can not understand where is the problem. I have checked mysql module installed.

Please help to sort-out the problem.

Florian Margaine
61.1k15 gold badges94 silver badges120 bronze badges
asked Jul 25, 2012 at 9:42
3
  • @hvgotcodes as OP said, app.js:6 var client = mysql.createConnection({ ^ TypeError: Object # has no method 'createConnection' at Object. Commented Jul 25, 2012 at 12:44
  • error is in app.js:6 var client = mysql.createConnection({^ TypeError: Object #<Object> has no method 'createConnection' Commented Jul 26, 2012 at 6:15
  • can anyone help me to find out the solve the problem. Its urgent. Commented Aug 1, 2012 at 7:46

1 Answer 1

4

@Chandan, I had the same error (briefly)

TypeError: Object #<Object> has no method 'createConnection'

Checked https://github.com/felixge/node-mysql but no one else is reporting it as an issue. Installing the latest (alpha) version of the node mysql module solved the problem:

npm install [email protected]

YMMV. If you figure it out without updating your mysql module please inform us.

answered Aug 1, 2012 at 10:30

2 Comments

hi Ignaseo, Thanks for reply. Your suggestion works for me after update mysql module with alpha3. One thing the server works after my browser refreshed after first one done other wise not working. Is there any way to debug my .js code?
This answer helped me out. I was using MySQL with RailwayJS on Ubuntu, and updating the mysql package fixed the issue.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.