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 8557431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:34:16+00:00 2026-06-11T15:34:16+00:00

I am using the excellent caolan async module for nodejs: I have this code:

  • 0

I am using the excellent caolan “async” module for nodejs:

I have this code:

exports.manageComments = function(req, res) {
    var toDeleteIds = [];
    var deleteFunctions = [];
    if (req.body.doDelete) {
        toDeleteIds = req.body.doDelete;
    }
    var i;
    for ( i = 0; i < toDeleteIds.length; i++ ) {
        var deleteFunction = function(callback) {
            var id = toDeleteIds[i];
            console.log(id);
            Comment.findOne({_id:id}, function(err, found) {            
                if (!err) found.remove(callback);
            });
        }
        deleteFunctions.push(deleteFunction);
    }
    async.parallel(
        deleteFunctions,
        function(err,results) {
            exports.comments(req, res); //render a view
        }
    );
};

My array contains two elements but console.log() keeps telling me “undefined”.

What am I missing?

  • 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-11T15:34:17+00:00Added an answer on June 11, 2026 at 3:34 pm

    Your problem is with:

        var deleteFunction = function(callback) {
            var id = toDeleteIds[i];
    

    because at the time each callback function is executed, i will have the same value as toDeleteIds.length. A closure doesn’t “trap” the value that an outer variable had at the time it was created; it “traps” a reference to whatever value the outer variable has at the time it’s executed (which in this case won’t be until well after your for loop has finished.

    In order to “trap” the value of i at the time you create your callback function, you need to make i a parameter of a function that you call to create your callback function. You need something like

        var deleteFunction = makeDeleteFunction(i, callback);
    

    And then create a separate function outside the callback:

    function makeDeleteFunction(i, callback) {
        return function(callback) {
            var id = toDeleteIds[i];
            console.log(id);
            Comment.findOne({_id:id}, function(err, found){            
                if (!err) found.remove(callback);
            });
         };
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been using this excellent decorator for memoization, which I found on the
We are using this excellent plugin for some small sparkline charts. Issue I have
I have created a simple content rotator using the excellent Cycle.js plugin, this gets
I am writing Python code in Visual Studio 2010 using the excellent Python Tools
I'm using the excellent validate CFC by Ryan J. Heldt http://validation.riaforge.org/ but have a
I have recently started using the excellent Texter . I wanted to set up
Update below... Ok, ive got a problem here. Im using this excellent vimeo class
I am using the rather excellent IIS7 Rewrite module (V2), and want to create
I'm using the excellent Magellan navigation framework from Paul Stovell . When you have
I am using the excellent jQuery coverflow plugin ContentFlow but I have a problem

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.