How to refer to variables from initializers?

75 views
Skip to first unread message

davosian

unread,
Aug 9, 2013, 1:38:09 AM8/9/13
to locomo...@googlegroups.com

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


theoverl...@gmail.com

unread,
Nov 2, 2013, 9:36:17 PM11/2/13
to locomo...@googlegroups.com
Hi Dennis.

Did you ever find an solution to this?

Jim

Dennis Staiger

unread,
Nov 3, 2013, 3:16:35 AM11/3/13
to locomo...@googlegroups.com
Sadly no. I gave up on locomotive due to the lack of support and documentation...

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.


--

Dennis

http://www.davosian.net

theoverl...@gmail.com

unread,
Nov 3, 2013, 5:49:19 AM11/3/13
to locomo...@googlegroups.com
Thanks for your reply Dennis.

In case anyone else comes across this post, I worked out the solution:


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) {...


theoverl...@gmail.com

unread,
Nov 3, 2013, 5:49:44 AM11/3/13
to locomo...@googlegroups.com
Dennis - out of interest, what did you move to instead?
Reply all
Reply to author
Forward
0 new messages