1

I'm developing a node module. I need to pass the mongoose to my module to get three things (mongoose.connection, mongoose.connection.db, mongoose.mongo) out of it.

index.js (myModule - the module I developed)

function myModule(mongoose) {
    var db = mongoose.connection;
    var gfs = gridfsLockingStream(mongoose.connection.db, mongoose.mongo);
    this.someFunction = function() {//some code here}
}
module.exports = myModule;

db.js (A user must use myModule like this)

var mongoose = require('mongoose');
var myModule = require('myModule');

var dbUrl = 'mongodb://localhost:27017/gfsTestDB';
mongoose.connect(dbUrl);

var readyObj = new myModule(mongoose);
module.exports = readyObj; // so that the user can use this everywhere

Then the user can use readyObj to do his/her work. My problem is that only mongoose.connection is available in myModule function and I get this error(gridfsLockingStreamn cause the error):

Error: missing db argument

new Grid(db, mongo)

I'm using :

"mongodb": "3.0.4",

"mongoose": "4.11.6",

"gridfs-locking-stream": "1.1.1",

"gridfs-stream": "1.1.1",

One solution (idea from @GrégoryNEUT) (but I think it's not the correct way):

index.js no changes

db.js using promise and mongoose event handler

var mongoose = require('mongoose');
var myModule = require('myModule');

var dbUrl = 'mongodb://localhost:27017/gfsTestDB';
mongoose.connect(dbUrl);


module.exports = new Promise(function (resolve, reject) {

    mongoose.connection.on('connected', function () {
        var readyObj = new myModule(mongoose);
        resolve(readyObj);
    });
});

photoRouter.js (one of the user's files - the user want to use readyObj)

var readyObj = require('./db').then(function (readyObj) {
    // then the user uses readyObj
}

Can the code be improved?

Community
  • 1
  • 1
Kasra GH
  • 157
  • 2
  • 5
  • 22
  • I tried your code and it ran fine on my system without any errors. – ViKiG Mar 22 '18 at 14:48
  • @ViKiG Are you using the same version of the modules ? – Kasra GH Mar 22 '18 at 14:53
  • Except for the mongoose (4.9.x) everything else is same. I installed your version of `mongoose`, still same result. There is probably something missing in your question. Post more code. – ViKiG Mar 22 '18 at 15:08
  • 1
    Very strange! But I've tested it hundreds of time. Are you sure you're connected to your database ? (use db.on('open' , function(){}) – Kasra GH Mar 22 '18 at 15:15
  • 1
    I think @KasraGH you've pointed it out. `.connect()` is asynchronous. You should wait the connection to be opened before to reach `mongoose.connection.*` – Orelsanpls Mar 22 '18 at 16:43
  • @KasraGH I think Gregory is right. My `mongoose` is connected when I am running your `myModule` code. – ViKiG Mar 22 '18 at 16:51
  • @GrégoryNEUT Should I use promises, async module or something like those to overcome this issue? How can I know if the connection is established? – Kasra GH Mar 22 '18 at 17:31

1 Answers1

1

Looking at the documentation of mongoose connect


You can use of Promises.

    var mongoose = require('mongoose');
    var myModule = require('myModule');

    var dbUrl = 'mongodb://localhost:27017/gfsTestDB';

    mongoose.connect(dbUrl)
      .then(
        // The connection is ready to use!
        () => {
          var readyObj = new myModule(mongoose);

          // ...
        },

        // Handle the connection error
        (err) => {
          // ...
        },
      );

You can use of Callbacks

    var mongoose = require('mongoose');
    var myModule = require('myModule');

    var dbUrl = 'mongodb://localhost:27017/gfsTestDB';

    mongoose.connect(dbUrl, (err) => {
      if (err) {
        // Handle the error

        // ...

        return;
      }

      // We get successfully connected to the database

      var readyObj = new myModule(mongoose);

      // ...
    });
Orelsanpls
  • 22,456
  • 6
  • 42
  • 69
  • Perfect, but one more thing. I want to export the **readObj** like: _module.exports = readyObj_ . How can I do this? readyObj is now a local variable and is not accessible outside the mongoose success callback function. – Kasra GH Mar 22 '18 at 18:09