I'm following the tutorial but first i got this error:
deprecated: connect() is now done automatically
I changed my code but after that im faceing this error:
object is not a function at Client.CALL_NON_FUNCTION_AS_CONSTRUCTOR (native)
and I've no idea as I'm new to nodejs and mysql so kindly help me out!!
here is my code:
var Client = require('mysql').createClient({'host':'localhost','port':'3306','user':'root', 'password':''});
var client = new Client();
console.log('Connecting to mysql..');
ClientConnectionReady(client);
ClientConnectionReady = function(client){
client.query('USE login',function(err,result){
if(err){
console.log('cant create Table Error:'+ err.message);
client.end();
return;
}
ClientReady(client);
};
ClientReady = function(client){
var values = ['fahad','tariq'];
client.query('INSERT into Login SET username = ?, password = ?',values,
function(err,result){
if(err){
console.log("ClientReady Error:" + error.message);
client.end();
return;
}
console.log('inserted:'+result.affectedRows+'ros.');
console.log('Id inserted:'+ result.insertId);
});
GetData(client);
}
GetData = function(client){
client.query(
'SELECT * FROM Login',
function selectCb(err,result,fields){
if(err) {
console.log('Getdata Error'+err.message);
client.end();
return;
}
if(result.length>0) {
var firstResult = results[0];
console.log('username' + firstResult['username']);
console.log('password'+firstResult['password']);
}
});
client.end();
console.log('connection closed');
};
Not sure this is what's causing your specific message, but in ClientReady
you're calling GetData
before your INSERT
has had a chance to execute; you should be doing it inside the callback from the INSERT
.
UPDATE: Now that someone was kind enough to properly indent everything (hint: stuff that should be indented should begin with 4 spaces) it becomes clear that you have a similar problem in GetData()
: you're calling client.end()
before your callback has had a chance to execute. Thus your SELECT
callback is trying to use a closed MySQL connection, which is probably causing the failure.