摩卡-手表和猫鼬模型 [英] mocha --watch and mongoose models

查看:104
本文介绍了摩卡-手表和猫鼬模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我离开mocha监视更改,每次保存文件时,猫鼬都会抛出以下错误:

If I leave mocha watching for changes, every time I save a file mongoose throws the following error:

OverwriteModelError:编译后无法覆盖Client模型

我知道猫鼬不允许两次定义模型,但是我不知道如何使它与mocha --watch一起使用.

I know that mongoose won't allow to define a model twice, but I don't know how to make it work with mocha --watch.

// client.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var clientSchema = new Schema({
    secret: { type: String, required: true, unique: true },
    name: String,
    description: String,
    grant_types: [String],
    created_at: { type: Date, default: Date.now }
});

module.exports = mongoose.model('Client', clientSchema);

这是测试

// client-test.js
var chai = require('chai');
var chaiHttp = require('chai-http');
var mongoose = require('mongoose');

var server = require('../../app');
var Client = require('../../auth/models').Client;

var should = chai.should();

chai.use(chaiHttp);

describe('client endpoints', function() {
    after(function(done) {
        mongoose.connection.close();
        done();
    });

    it('should get a single client on /auth/client/{clientId} GET', function(done) {
        var clt = new Client({
            name: 'my app name',
            description: 'super usefull and nice app',
            grant_types: ['password', 'refresh_token']
        });

        clt.save(function(err) {
            chai.request(server)
                .get('/auth/client/' + clt._id.toString())
                .end(function(err, res) {
                    res.should.have.status(200);
                    res.should.be.json;
                    res.body.should.have.property('client_id');
                    res.body.should.not.have.property('secret');
                    res.body.should.have.property('name');
                    res.body.should.have.property('description');
                    done();
                });
        });
    });
});

推荐答案

我遇到了同样的问题.我的解决方案是检查是否已创建/编译模型,如果没有创建,则只需检索模型.

I had the same issue. My solution was to check whether the model was created/compiled yet, and if not then do so, otherwise just retrieve the model.

使用 mongoose.modelNames(),您可以获取您的模型名称.然后使用.indexOf检查您要获取的模型是否在数组中.如果不是,则编译模型,例如:mongoose.model("User", UserSchema),但是如果已经定义了模型(与mocha --watch一样),只需检索模型(不要再次编译),即可可以使用例如mongoose.connection.model("User").

using mongoose.modelNames() you can get an array of the names of your models. Then use .indexOf to check if the model you want to get is in the array or not. If it is not, then compile the model, for example: mongoose.model("User", UserSchema), but if it is already defined (as is the case with mocha --watch), simply retrieve the model (don't compile it again), which you can do with for example: mongoose.connection.model("User").

这是一个函数,该函数返回执行此检查逻辑的函数,该函数本身将返回模型(通过编译或仅检索模型).

This is a function which returns a function to do this checking logic, which itself returns the model (either by compiling it or just retrieving it).

const mongoose = require("mongoose");
//returns a function which returns either a compiled model, or a precompiled model
//s is a String for the model name e.g. "User", and model is the mongoose Schema
function getModel(s, model) {
  return function() {
    return mongoose.modelNames().indexOf(s) === -1
      ? mongoose.model(s, model)
      : mongoose.connection.model(s);
  };
}
module.exports = getModel;

这意味着您必须对模型有所不同,因为您可能会替换以下内容:

This means you have to require your model a bit differently, since you are likely replacing something like this:

module.exports = mongoose.model("User", UserSchema);

返回模型本身, 与此:

which returns the model itself, with this:

module.exports = getModel("User", UserSchema);

返回一个函数以返回模型,方法是编译模型或仅检索模型.这意味着当您需要用户"模型时,您将要调用getModel返回的函数:

which returns a function to return the model, either by compiling it or just retrieving it. This means when you require the 'User' model, you would want to call the function returned by getModel:

const UserModel = require("./models/UserModel")();

我希望这会有所帮助.

这篇关于摩卡-手表和猫鼬模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆