javascript - node-mysql timing -


i have recursive query (note: example):

var user = function(data) {   this.minions = [];   this.loadminions = function()   {     _user = this;     database.query('select * users 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 minions");   } }  currentuser = new user(id);  (var m in currentuser.minions)  {    console.log("minion found!");  } 

this don't work because timmings wrong, code don't wait query.

i've tried 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 * users 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 minions");   } } currentuser = new user(id); (var m in currentuser.minions) {   console.log("minion found!"); } 

but freezes on while, missing something?

the first hurdle solving problem understanding i/o in node.js asynchronous. once know how applies problem recursive part easier (especially if use flow control library async or step).

here example of you're trying (minus recursion). personally, avoid recursively loading possibly unknown number/depth of records that; instead load them on demand, in example:

var user = function(data) {     this.data = data     this.minions; };  user.prototype.getminions = function(primarycallback) {     var = this; // scope handle     if(this.minions) { // bypass db query if results cached         return primarycallback(null, this.minions);     }      // callback invoked database.query when has records     var acallback = function(error, results, fields) {         if(error) {             return primarycallback(error);         }          // put recursive minion initialization         // problem going have callback counting, using library         // async or step make party much easier          that.minions = results; // bypass db query after         primarycallback(null, results);     }      database.query('select * users owner = ' + data.id, acallback); };  var user = new user(somedata);     user.getminions(function(error, minions) {     if(error) {         throw error;     }      // inside function invoked primarycallback(...)     minions.foreach(function(minion) {         console.log('found minion:', minion);     }); }); 

the biggest thing note in example callbacks. database.query(...) asynchronous , don't want tie event loop waiting finish. solved providing callback, acallback, query, executed when results ready. once callback fires , after perform whatever processing want on records can fire primarycallback final results.


Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -