简体   繁体   English

NodeJS中的MongoClient.connect阻塞:async.map()

[英]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. 我有三个数据库,每个数据库在MongoDB中都有一个名为“ items”的集合,我想从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. 开始连接之前,我获得了一个包含这些数据库名称的数组,然后使用async.map()为这些数据库中的每个数据库创建连接。 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. 以下是我的coffeescript代码。

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. 我认为封锁是由于mongo客户所致,但我不确定该如何解决。

That's expected since you have open network connections now to your mongo databases. 这是预料之中的,因为您现在已经打开了与mongo数据库的网络连接。 If you close them you should find your process will exit naturally. 如果关闭它们,您会发现您的过程将自然退出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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