0

I'm a beginner with Node.js, is it ever ok to use MySQL as a global var?

I have a db_helper.js with this code inside:

global.client = require('mysql').createConnection({
    user: '__mysqluser__',
    password: '__mysqlpass__',
    database: '__mysqldb__',
    timezone: '-03:00'
});
global.client.connect();

On my main.js I just do a:

require('db_helper');

Then on other js files, whenever I need to UPDATE or SELECT I just call:

global.client(query, data);

I haven't seen any code like this yet, but it works as expected, but I'm experiencing random crashes from time to time, when reloading pages.

Is it OK to use it like this? Are my crashes related to the way I connect to the DB?

I think it's related because when the crash happens, is because MySQL fails to return data, but the crash happens when parsing the result, like:

global.client.query(query, function(err, results, fields) {
   if (err) throw err;
   if (results && Object.prototype.toString.call(results) === '[object Array]') {
      var j = result[0].data; 
   }
}

Most of the time, var j has the value I expect it to have, but when node crashes, this var returns empty, despite the fact that it checks for (results) consistency before. Node crashes saying:

result[0].data is undefined

Thanks.

EDIT: The crash happens whenever the client js file gets updated on server. (after a local edit of the .js file & server upload via FTP), Why?

Antonio Max
  • 8,627
  • 6
  • 43
  • 42

1 Answers1

5

A more common approach, instead of storing stuff in global (which makes for obscure code, IMO), would be to export it:

// db_helper.js
var client = module.exports = require('mysql').createConnection({
    user: '__mysqluser__',
    password: '__mysqlpass__',
    database: '__mysqldb__',
    timezone: '-03:00'
});
client.connect();

// somewhere else
var client = require('db_helper');

The reason why your code crashes is that you're not checking for an empty array:

client.query(query, function(err, results, fields) {
   if (err) throw err;
   if (results.length) { // don't need to check if it's an array
     var j = results[0].data;
     ...
   }
}

As for the reason: I would guess that the query you're running gives different results (or rather, no results) on your server.

robertklep
  • 198,204
  • 35
  • 394
  • 381
  • Mind to expand on why I should not use the global var? I mean, it exists for a reason and it works as expected without using exports (thats a nice approach btw). – Antonio Max Jul 18 '13 at 00:01
  • @AntonioMax one of the main issues I have with global variables is that they can be defined anywhere, making it really hard to track where they come into existence. For a small project that might not be an issue, but in larger projects it can. – robertklep Jul 18 '13 at 05:08
  • Yes it's a very small project, about 70 lines of code, really neat stuff this node.js. Thank you for your answer anyway. I have a temporary fix for the crashings using mysql pooling and long pooling. I'll update the original post with it later, thanks. – Antonio Max Jul 21 '13 at 02:01