node.js 猫鼬试图打开未关闭的连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15712226/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:03:34  来源:igfitidea点击:

Mongoose Trying to open unclosed connection

node.jsmongodbmongoose

提问by Ben Wong

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." error.

这是问题的简化版本,但基本上我试图用猫鼬打开 2 个 mongodb 连接,它给了我“试图打开未关闭的连接”。错误。

Code sample:

代码示例:

var db1 = require('mongoose');
db1.connect('my.db.ip.address', 'my-db');

var db2 = require('mongoose');
db2.connect('my.db.ip.address', 'my-db');

db2.connection.close();
db1.connection.close();

Any idea how to make it work?

知道如何使它工作吗?

回答by Raghuveer

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

connect()打开到数据库的默认连接。由于您需要两个不同的连接,因此请使用createConnection().

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

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

回答by slorenzo

I had this problem doing unit test with mocha.

我在使用mocha.

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

当我添加第二个测试时出现问题,因为beforeEach被调用了两次。

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!

希望对你有帮助!

回答by Thierry

To add on Raghuveer answer :

添加 Raghuveer 答案:

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(...);

回答by ArchNoob

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
}

回答by Moacir Rosa

Using mongoose.disconnect(fn):

使用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.

我发现这个问题输入了错误消息,尽管我的问题有点不同,但我相信它对那些使用 Hapi 的人有用。更具体地说,Hapi+ rest-hapi+ mocha

When running mochawith --watchoption I was facing both: OverwriteModelErrorand Error: Trying to open unclosed connection errors.

mocha使用--watch选项运行时,我同时面临:OverwriteModelErrorError: Trying to open unclosed connection errors.

回答by Edwin Ikechukwu Okonkwo

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');