I have set up my database connection to mysql in an initializer:
module.exports = function() {
this.mysql = require('mysql');
this.connection = this.mysql.createConnection(this.set('connection_details'));
this.connection.connect();
}
Now I would like to use this connection in a model (or controller), but the following code inside a controller gives me "undefined" for the connection object:
var locomotive = require('locomotive')
, Controller = locomotive.Controller;
var PagesController = new Controller();
PagesController.main = function() {
this.title = 'Locomotive'
this.connection.query('SELECT 1 + 1 AS solution', function(err, rows, fields) {
if (err) throw err;
console.log('The solution is: ', rows[0].solution);
});
this.connection.end();
this.render();
}
module.exports = PagesController;
How do I proplerly access or declare the connection variable?
Thanks for your help.
Regards,
Dennis
--
You received this message because you are subscribed to a topic in the Google Groups "Locomotive" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/locomotivejs/VB1eq_Yv78Y/unsubscribe.
To unsubscribe from this group and all its topics, send an email to locomotivejs...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
In Dennis's example, rather than:
this.connection.query('SELECT 1 + 1 AS solution', function(err, rows, fields) {...
it should read:
this.app.connection.query('SELECT 1 + 1 AS solution', function(err, rows, fields) {...