0

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)

Shubham Verma
  • 8,783
  • 6
  • 58
  • 79
Patrick Ian Co
  • 315
  • 1
  • 2
  • 14

2 Answers2

0

What contains mongoUrl ?

You sure that conn.host is different for each connections ? Otherwise it set the value in the same object keys

====================================

mongoose.connection.openUri probably don't create another connection each time. I think it must use the same connection object.

Try to create a real new mongo connection with :

mongoose.createConnection()

It will return a new connection object for each connections

Daphoque
  • 4,421
  • 1
  • 20
  • 31
  • It's an array of MongoDB URIs, yep I logged the conn object after creation and it created two 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) – Patrick Ian Co Jul 12 '18 at 08:54
  • see edit, you have to call createConnection instead of openUri for each connection wanted – Daphoque Jul 12 '18 at 09:13
  • But this will return an error _Db.prototype.authenticate method will no longer be available in the next major release 3.x as MongoDB 3.6 will only allow auth against users in the admin db and will no longer allow multiple credentials on a socket. Please authenticate using MongoClient.connect with auth credentials._ – Patrick Ian Co Jul 12 '18 at 09:41
  • And with this : var mongoose = require("mongoose"); var dbA = new mongoose.Mongoose(); var dbB = new mongoose.Mongoose(); var connA = dbA.connect("mongodb://localhost:27017/a"); var connB = dbB.connect("mongodb://localhost:27018/b"); – Daphoque Jul 12 '18 at 09:54
0

You should try this:

async function createMongooseConnctions(mongoUrl, opts){
       var mongooseConnections = {};

       for(const value of mongoUrl){
          (function(){
               var conn = await mongoose.connection.openUri(value, opts)
               let host = conn.host
               mongooseConnections[host] = conn
         })(value)
       }
    }

Hope it will help you.

Shubham Verma
  • 8,783
  • 6
  • 58
  • 79