Hi Peter,
As in my app, I'musing mySQL, I want to avoid redefining the connection every time, so thought to get it defined globally, and call it whenever needed, below the statement I used, along with the definition file.
The SQL is executed, and results appeared in the console, but not sent back to the controller, neither self.json, nor return, did work... any thought pls.
thanks
function items_json_data(id) {
var self = this;
var SQL;
SQL="SELECT * from users where idusers = "+id.toString();
var SQLresults = framework.global.executeSQL(SQL);
console.log(SQLresults);
definitions/global.js
var mysql = require('mysql');
// FRAMEWORK GLOBAL VARIABLE
framework.global.executeSQL = function(SQL) {
var self = this;
var connection = self.database('test');
connection.connect(function(err) {
connection.query(SQL,function (err, rows) {
if (err) {self.view500(err); return; }
console.log(rows);
self.json(rows); //--> Did not work
return rows; //--> Did not work
connection.end();
});
});
}
framework.global.database = function (name) {
var self=this;
// Create a connection to MySql Server and Database
return new mysql.createConnection({
host: 'localhost',
port: 3306,
database: name,
user: 'root',
password: 'myPSWD'
});
};