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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:00:34+00:00 2026-06-15T07:00:34+00:00

I am using async.js to run a for loop over a collection. For performance

  • 0

I am using async.js to run a for loop over a collection. For performance reasons, I want to pass in a database connection into the iterator method, so that it’s not opening/closing a db connection each time the iterator runs. I am using mongoose.js for my data models.

The code below gets all the Artists in mongo, and then adds a song for each. My question is, how can I use the same db connection from updateAllArtists in addArtistSong?

function updateAllArtists() {
    var db = mongoose.createConnection('localhost/dbname');
    var Artist = db.model('Artist', artistSchema);
    Artist.find({}, function(err, artists) {
       // for each artist, add a song
       async.forEach(artists, addArtistSong, function(err) {
    });
}

function addArtistSong(artist, cb) {
    // THIS IS WHERE I NEED A DB CONNECTION
    var Song = db.model('Song', songSchema);
}

Can I extend the iterator signature somehow, like addArtistSong(artist, db, cb)? Then how would I pass this in from the forEach call?

  • 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-15T07:00:36+00:00Added an answer on June 15, 2026 at 7:00 am

    One option is to make the db connection a global variable. But if you don’t want that you can always use a closure to create a shared variable between functions:

    (function(){
      // create db in this closure
      var db = mongoose.createConnection('localhost/dbname');
    
      function updateAllArtists() {
        // now db is available here
        var Artist = db.model('Artist', artistSchema);
        Artist.find({}, function(err, artists) {
          // for each artist, add a song
          async.forEach(artists, addArtistSong, function(err) {});
        });
      }
    
      function addArtistSong(artist, cb) {
        // and also available here
        var Song = db.model('Song', songSchema);
      }
    })()
    

    Another option is to pass it as a parameter to addArtistSong. Since async.forEach expect the iterator function to accept only 2 parameters we can use an anonymous function wrapper to pass 3 parameters to addArtistSong:

    function addArtistSong(db,artist,callback) {
        db.model(); //...
    }
    

    and calling it in async.forEach:

    async.forEach(
      artists,
      function(x,cb){addArtistSong(db,x,cb)},
      function(err) {}
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using async task to create loading screen, but when I want to pass
Take the following naive implementation of a nested async loop using the ThreadPool: ThreadPool.SetMaxThreads(10,
how to run the async task at specific time? (I want to run it
Would be glad to see some feedback from you in using async ctp in
I am doing an HttpPost to get data from a php server using async
From what I've seen about using the Async CTP with the event asynchronous pattern,
I'm trying to download strings from a HttpPost and I'm using the Async class
I'm having trouble using the new async/await tools in c#. Here is my scenario:
I am using the node.js async package, specifically forEachSeries, to make a series of
When merging JPA entities using JpaTemplate in methods marked with @Async, the entity does

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.