I'm having a problem with asynchronous tasks while creating Mongoose Connections. I will be having an n number of connections
stored in an array and I want to push each connection object
that was created into a single object
.
In my code, only the last connection
is pushing to the object
.
My code:
async function createMongooseConnctions(mongoUrl, opts){
var mongooseConnections = {};
for(const value of mongoUrl){
var conn = await mongoose.connection.openUri(value, opts)
let host = conn.host
mongooseConnections[host] = conn
}
}
I'm also having a hard time pushing it to my object because I can't seem to replicate the code that was used to make dynamic keys. If there is any other way from what I've been doing that is more efficient would be great as well.
EDIT:
I logged the conn object after creation and it created n number of different connections objects, but when I'm trying to push them to my JSON Object, the last connection is the only one pushed (n number of times depending on the number of uri)