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?

Answers


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.


Each Node.js process is single-threaded, so the line

while(Done != true){};

takes over the thread, and the callback that would have set Done to true never gets run because the thead is blocked on an infinite loop.

You need to refactor your program so that code that depends on the results of the query is included within the callback itself. For example, make MyQuery take a callback argument:

MyQuery = function(QueryString, callback){

Then call the callback at the end of your database.query callback -- or even supply it as the database.query callback.


The freezing is unfortunately correct behaviour, as Node is single-threaded.

You need a scheduler package to fix this. Personally, I have been using Fibers-promise for this kind of issue. You might want to look at this or another promise library or at async


Need Your Help

Java mail Api ,unable to read ".msg attachment" from outlook client

java java-ee javamail

I've a class which reads email using the javamail api, the problem I'm facing is that when I send a mail containing a .msg attachement from gmail,my code can read it easily,but when I send the same...

DBMS_PROFILER - only anonymous

oracle plsql oracle11g

I am trying to get over with DBMS_PROFILER, but I face a problem, that I can't get through.