framework.global

16 views
Skip to first unread message

Hasan Yousef

unread,
Jan 10, 2014, 5:22:12 AM1/10/14
to part...@googlegroups.com
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'
    });
};


Peter Širka

unread,
Jan 10, 2014, 8:23:39 AM1/10/14
to part...@googlegroups.com
Hi Hasan,
it's bad. Why do you not want to rewrite framework.database prototype?

Thanks :-)
Reply all
Reply to author
Forward
0 new messages