Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8091555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:05:31+00:00 2026-06-05T20:05:31+00:00

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

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T20:05:31+00:00Added an answer on June 5, 2026 at 8:05 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a recursive data type like this: template<typename T> struct SomeType { std::map<T,
I have a list of hierarchical data like this: var list = new List<Data>(){some
I have a simple recursive array function that looks like this: function recursive_array($results) {
I have a recursive model like this: public class Node { public int Id
I have a recursive function that returns a value. public function getparent ($user) {
I have a linq to objects query in a recursive loop and afraid when
I have a recursive function working on a list, the function contains a loop
I have this recursive method which deletes empty folders: private void DeleteEmpty(DirectoryInfo directory) {
i have this recursive code <ul> <% sql=SELECT * FROM cats ORDER BY orderid
I have a recursive function reading a table of contents of documents from a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.