繁体   English   中英

mongo模式不使用默认的猫鼬连接

[英]mongo schema not using default mongoose connection

考虑以下两个文件...

// orgSchema.js
var OrgSchema = new Schema({
   ...
});

exports.OrgDB = mongoose.model('Organization', OrgSchema);


// orgs.spec.js
var OrgDB = require('...').OrgDB;

describe('organizations', function() {
    before(function(done) {
        mongoose.Promise = Promise;
        mongoose.connect('...', done);
    });

    after(function(done) {
        mongoose.connection.close(function() {
            done();
        });
    });

    describe('simple test', function (done) {
        var org = new OrgDB();

        org.name = 'New Org';
        org.save(function(err) {
            if (err) done(err);

            done();
        });
    });
});

注意,我有两个文件。 如果将架构​​与mocha单元测试放在同一文件中,则一切正常。 但是,就目前情况而言(架构位于其他文件中),架构模型(例如mongoose.model )无法识别默认连接,因此向我发出警告:

Mongoose: mpromise (mongoose's default promise library) is deprecated, plug in your own promise library instead: http://mongoosejs.com/docs/promises.html

和错误:

Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.

另外,如果我将mongoose.Promise and connect放在模型文件中mongoose.Promise and connect线,则一切正常。

如何获得使用/识别默认连接的架构?

如果它是一个不同的文件,那么您需要做的就是像使用模式那样导出连接。 我会做类似的事情:

const connection = mongoose.connect("mongourl")
mongoose.Promise = global.Promise
module.exports = connection

然后,我需要在文件中使用连接。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM