Search code examples
javascriptmysqlnode.jstimingnode-mysql

node-mysql timing


i have a recursive query like this (note: this is just an example):

var user = function(data)
{
  this.minions = [];
  this.loadMinions = function()
  {
    _user = this;
    database.query('select * from users where owner='+data.id,function(err,result,fields)
    {
      for(var m in result)
      {
        _user.minions[result[m].id] = new user(result[m]); 
        _user.minions[result[m].id].loadMinions();
      }
    }  
    console.log("loaded all minions");
  }
}
 currentUser = new user(ID);
 for (var m in currentUser.minions)
 {
   console.log("minion found!");
 }

this don't work because the timmings are all wrong, the code don't wait for the query.

i've tried to do this:

var MyQuery = function(QueryString){
    var Data;
    var Done = false;
    database.query(QueryString, function(err, result, fields) { 
        Data = result;
        Done = true;
    });
    while(Done != true){};
    return Data;
}

var user = function(data)
{
  this.minions = [];
  this.loadMinions = function()
  {
    _user = this;
    result= MyQuery('select * from users where owner='+data.id);
    for(var m in result)
    {
      _user.minions[result[m].id] = new user(result[m]); 
      _user.minions[result[m].id].loadMinions();
    }
    console.log("loaded all minions");
  }
}
currentUser = new user(ID);
for (var m in currentUser.minions)
{
  console.log("minion found!");
}

but he just freezes on the while, am i missing something?


Solution

  • The first hurdle to solving your problem is understanding that I/O in Node.js is asynchronous. Once you know how this applies to your problem the recursive part will be much easier (especially if you use a flow control library like Async or Step).

    Here is an example that does some of what you're trying to do (minus the recursion). Personally, I would avoid recursively loading a possibly unknown number/depth of records like that; Instead load them on demand, like in this example:

    var User = function(data) {
        this.data = data
        this.minions;
    };
    
    User.prototype.getMinions = function(primaryCallback) {
        var that = this; // scope handle
        if(this.minions) { // bypass the db query if results cached
            return primaryCallback(null, this.minions);
        }
    
        // Callback invoked by database.query when it has the records
        var aCallback = function(error, results, fields) {
            if(error) {
                return primaryCallback(error);
            }
    
            // This is where you would put your recursive minion initialization
            // The problem you are going to have is callback counting, using a library
            // like async or step would make this party much much easier
    
            that.minions = results; // bypass the db query after this
            primaryCallback(null, results);
        }
    
        database.query('SELECT * FROM users WHERE owner = ' + data.id, aCallback);
    };
    
    var user = new User(someData);    
    user.getMinions(function(error, minions) {
        if(error) {
            throw error;
        }
    
        // Inside the function invoked by primaryCallback(...)
        minions.forEach(function(minion) {
            console.log('found this minion:', minion);
        });
    });
    

    The biggest thing to note in this example are the callbacks. The database.query(...) is asynchronous and you don't want to tie up the event loop waiting for it to finish. This is solved by providing a callback, aCallback, to the query, which is executed when the results are ready. Once that callback fires and after you perform whatever processing you want to do on the records you can fire the primaryCallback with the final results.