简体   繁体   中英

MongoClient.connect blocking in NodeJS: async.map()

I have three databases, each with a collection called 'items' in MongoDB which I would like to connect to from node.js. Before starting the connection, I obtain an array containing the names of these databases and then I use async.map() to create the connections for each of these databases. When the final callback is executed, all of the connections are open, but the process seems to be blocked and doesn't proceed any further. Below is my coffeescript code.

fs = require 'fs'
jf = require 'jsonfile'
MongoClient = (require 'mongodb').MongoClient
async = require 'async'

getConfigFileName = () ->
    process.argv[2]

transformed = (err, transformed) ->
    console.log transformed

connectMongoDB = (dbEntry, callback) ->
    MongoClient.connect "mongodb://localhost:12345/" + dbEntry.databaseName, (err, db) ->
        if err
            callback err, dbEntry
        else
            dbEntry.connection = db
            callback null, dbEntry

# Start Execution Here.
configFileName = getConfigFileName()

databases = jf.readFileSync configFileName

async.map databases, connectMongoDB, transformed

I believe the blocking occurs due to the mongo client, but I'm unsure what to do to solve this problem.

That's expected since you have open network connections now to your mongo databases. If you close them you should find your process will exit naturally.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM