Mongoose Trying to open unclosed connection

前端 未结 6 431
离开以前
离开以前 2020-12-24 05:38

This is a simplified version of the problem, but basically I\'m trying to open 2 mongodb connections with mongoose and it\'s giving me \"Trying to open unclosed connection.\

相关标签:
6条回答
  • 2020-12-24 06:09

    I had this problem doing unit test with mocha.

    The problem came when I added a second test because beforeEach is called twice.

    I've solved this with this code:

    const mongoose = require('mongoose');
    describe('Your test suite', () => {
        beforeEach( () => {
            if (mongoose.connection.db) {
                return; // or done();
            } else {
                // connect to mongodb
        });
    
        describe('GET /some-path', () => {
           it('It should...', () => {
    
           });
        });
    
        describe('POST /some-path', () => {
           it('It should...', () => {
    
           });
        });
    });
    

    Hope it helps you!

    0 讨论(0)
  • 2020-12-24 06:12

    I get this issue while running my tests.

    This is what I did to solve it.

    //- in my app.js file.
    try {
        mongoose.connect('mongodb://localhost/userApi2'); //- starting a db connection
    }catch(err) {
        mongoose.createConnection('mongodb://localhost/userApi2'); //- starting another db connection
    }

    0 讨论(0)
  • 2020-12-24 06:14

    Using mongoose.disconnect(fn):

    mongoose.disconnect(() => {
    
      // here it would be possible "reset" models to fix 
      // OverwriteModelError errors
      mongoose.models = {};
    
      // here comes your logic like registering Hapi plugins
      server.register(somePlugin, callback);
    });
    

    I found this question typing the error message and despite my problem is a bit different I believe it could be useful for those using Hapi. More specifically Hapi + rest-hapi + mocha.

    When running mocha with --watch option I was facing both: OverwriteModelError and Error: Trying to open unclosed connection errors.

    0 讨论(0)
  • 2020-12-24 06:27

    connect() opens the default connection to the db. Since you want two different connections, use createConnection().

    API link: http://mongoosejs.com/docs/api.html#index_Mongoose-createConnection

    0 讨论(0)
  • 2020-12-24 06:29

    To add on Raghuveer answer :

    I would also mention that instead of using mongoose directly (you are probably using it this way you end up on this post) :

    require('mongoose').model(...);
    

    You would use the returned connection :

    var db = require('mongoose').connect('xxx', 'yyy');
    db.model(...);
    
    0 讨论(0)
  • 2020-12-24 06:29

    You are attempting to open the default connection ( which is not yet closed ) a 2nd time.

    do the following instead

    var db = require('mongoose'); //note only one 'require' needed.
    var connectionToDb1 = db.createConnection('my.db1.ip.address', 'my-db1');
    var connectionToDb2 = db.createConnection('my.db2.ip.address', 'my-db2');
    
    0 讨论(0)
提交回复
热议问题