gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 01:05:10 25 4
gpt4 key购买 nike

我有三个数据库,每个数据库都有一个 MongoDB 中名为“items”的集合,我想从 node.js 连接到该集合。在开始连接之前,我获取一个包含这些数据库名称的数组,然后使用 async.map() 为每个数据库创建连接。当执行最终回调时,所有连接都打开,但该过程似乎被阻止并且不再继续进行。下面是我的 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

我相信阻塞是由于 mongo 客户端造成的,但我不确定如何解决这个问题。

最佳答案

这是预期的,因为您现在已经打开了与 mongo 数据库的网络连接。如果您关闭它们,您应该会发现您的进程会自然退出。

关于javascript - NodeJS 中的 MongoClient.connect 阻塞 : async. map(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23712295/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com