Tuesday, 6 June 2017

node.js driver for mysql works in console but not in actual code

I have .js file with following code:

var mysql = require('mysql');
var connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'password',
database: 'databasename'});

function select(id,tablename){
connection.connect();
connection.query('select ' +tablename+'.property from databasename.'+tablename+' where id = '+id, function(err,result){
var string=JSON.stringify(result[0].property);
// alert(string); or console.log(string); 
});
connection.end();
}

// function go(){select(3,"tablename");} or select(3,"tablename"); 

where function.go() is called from button.onClick(). And when I run it from console via "node filename.js" it works fine and shows correct data, but it doesn't work from button.onClick().

What am I doing wrong?



via Mishacheng

No comments:

Post a Comment