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

  • SEARCH
  • Home
  • 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 8606891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:12:30+00:00 2026-06-12T03:12:30+00:00

Here I’m trying to make an array of functions with arguments to Async.js. The

  • 0

Here I’m trying to make an array of functions with arguments to Async.js.

The array consists of instances of RunRequest that are supposed to be set inside the loop in MakeRequest, right before I try pass the function array to Async.

So the request in request[i] is fine when I pass it to RunRequest, but inside RunRequest function its undefined?

// Process Requests
function RunRequest(db, collection, request, requestHandler, callback) {
    console.log('this happening?')
    // Connect to the database
    db.open(function(err, db) {
        if(err) callback(err, null);
        // Connect to the collection
        db.collection(collection, function(err, collection) {
            if (err) callback(err, null);
            // Process the correct type of command
            requestHandler(db, collection, request, callback);  
        });
    });
}

function MakeRequest(request, requestHandler, collection, callback) {
    var data = [];
    var doneRequest = function(err, results) {
        console.log('done was called')
        if (err) callback(err, null);
        else if(results) data = data.concat(results);
    }
    // Make Request Array
    var requestArray = [];
    for(var i = 0; i < request.length; i++) {
        console.log('run request was called')
        var dbConnection = new Db('KidzpaceDB', new Server(Host, Port, {auto_reconnect: true}))
        requestArray.push(function() {RunRequest(dbConnection, collection, request[i], requestHandler, doneRequest)});
    }
    // Make all requests in Parallel then invoke callback
    Async.parallel(requestArray, function(err, results) {
        console.log('Step WORKS')
        if(data) {
            var uniqueResults = [];
            for(var i = 0; i < data.length; i++) {
                if( !uniqueResults[data[i]['_id']] ) {
                    uniqueResults[uniqueResults.length] = data[i];
                    uniqueResults[data[i]['_id']] = true;
                }
                callback (null, uniqueResults);
            }
        }
    });
}


// Request Handlers
var FindHandler = function(db, collection, request, callback) {
    console.log('FindHandler was called')
    console.log('Request Query' + request);
    collection.find(request.query, function(err, cursor) {
        if (err) callback(err, null);
        cursor.toArray(function(err, docs) {
            if (err) callback(err, null);
            if(docs.length <= 0) console.log("No documents match your query");
            var requestResults = [];
            for(var i=0; i<docs.length; i++) {
                requestResults[requestResults.length] = docs[i]; 
            }
            db.close();
            callback(null, requestResults);
        });
    });
}
  • 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-12T03:12:31+00:00Added an answer on June 12, 2026 at 3:12 am

    This is a scoping issue. When the loop finishes variable i is set to request.length, so request[i] is undefined.

    Wrap your code with anonymous function like that:

    var requestArray = [];
    for(var i = 0; i < request.length; i++) {
        (function(i) {
            console.log('run request was called');
            var dbConnection = ...;
            requestArray.push( ... );
        })(i);
    }
    

    or even better ( avoids unnecessary overhead when creating anonymous functions ):

    var requestArray = [];
    request.forEach( function( el ) {
        console.log('run request was called');
        // the other code goes here, use el instead of request[i]
    });
    

    EDIT The callback is not called, because you don’t define functions in arrays correctly. You will have to refactor your code a bit, so let me just show you how it should be:

    requestArray.push(function(callback) { // <---- note the additional parameter here
        // do some stuff, for example call db
        db.open(function(err, db) {
            if (err) {
                callback( err );
            } else {
                callback( );
            }
        });
    });
    

    If you want to use RunRequest, then you need to pass callback as an additional parameter to RunRequest ( so use callback instead of doneRequest ).

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

Sidebar

Related Questions

Here's what I'm trying to accomplish with this program: a recursive method that checks
Here is the problem that I am trying to solve. I have two folders
Here is the code in a function I'm trying to revise. This example works
Here is an example: I write html code inside of textarea, then I swap
Here is the css: #content ul { font-size: 12px; } I am trying this:
here is my php code $titikPetaInti = array(); while($row = mysql_fetch_assoc($hasil2)) { $titikPetaInti[] =
Here is the code I'm using inside my AsyncTask DefaultHttpClient httpClient = new DefaultHttpClient();
Here is my program to find all the subsets of given set. To solve
Here's the code require_once 'functions.php'; require_once 'cfg.php'; $mysqli = new mysqli($dbhost, $dbuser, $dbpass, $db);
Here is my scenario. I have a website running under AppPool1 and that works

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.