为什么我会收到错误“一旦编译就无法覆盖模型"?当我第二次运行测试时在猫鼬? [英] Why do I get error "Cannot overwrite model once compiled" in Mongoose when I run my test a second time?

查看:29
本文介绍了为什么我会收到错误“一旦编译就无法覆盖模型"?当我第二次运行测试时在猫鼬?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读相关帖子:

我的模型如下所示:

<块引用>

forums.js

const mongoose = require("mongoose");const Schema = mongoose.Schema;const topicGroupSchema = require("./topicGroups");const 论坛架构 = 新架构({标题:字符串,主题组:[topicGroupSchema]})const Forum = mongoose.model("forums", forumSchema);module.exports = 论坛;

<块引用>

topicGroups.js

const mongoose = require("mongoose");const Schema = mongoose.Schema;const topicGroupSchema = 新架构({标题:字符串,//待办事项:添加主题})module.exports = topicGroupSchema;

我的 test_helper 和 saveForum_test.js 文件如下所示:

<块引用>

saveForum_test.js

const assert = require("assert");const Forum = require("../model/forums")describe("创建记录", () => {它(可以保存一个新论坛",(完成)=> {const 论坛 = 新论坛({标题:代码集线器"})论坛.save().then(() => {断言(forum.isNew)完毕();})})})

<块引用>

test_helper.js

const mongoose = require("mongoose");猫鼬.Promise = global.Promise;之前(完成 => {mongoose.connect("mongodb://myuser:mypassword@ds221339.mlab.com:21339/clicker", { useNewUrlParser: true });猫鼬连接.once("打开", () => {done()}).on("错误", 错误 => {控制台警告(错误",错误)})})//FIXME:保存两次时出错beforeEach(完成 => {console.log(mongoose.connection.collections.forums)mongoose.connection.dropCollection("论坛", () => {完毕();})})

因此,当我使用 mocha 运行我的测试套件时,一切都按预期进行.但是当我更改某些内容并再次运行时,我收到错误

<块引用>

OverwriteModelError:编译后无法覆盖forums模型.

我将 mlab 与 mongoose 一起使用,而不是本地安装的 mongodb.也许它与此有关?我想不通,我检查了所有文件和导入等 10 次都没有发现错误,你能吗?

解决方案

我已经解决了这个问题.

原来问题是我如何运行我的测试套件.我在 package.json 中的 npm run test 命令做了以下事情:

"test": "mocha --watch"

这是错误.我认为 --watch 不会重新实例化所有内容,就像热模块替换"一样.

我安装了 nodemon 并像这样更改了我的测试脚本:

"test": "nodemon --exec \"mocha -R min\""

这确保重新运行整个文件并且没有出现错误.

另一个应该起作用的相关帖子:mocha --watch 和猫鼬模型

I have read related post: Cannot overwrite model once compiled Mongoose

Problem is neither of these solution's helped me with my problem.

I get the error in the title and I have following setup:

Folder Structure:

My models look like this:

forums.js

const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const topicGroupSchema = require("./topicGroups");

const forumSchema = new Schema({
    title: String,
    topicGroups: [topicGroupSchema]
})

const Forum = mongoose.model("forums", forumSchema);

module.exports = Forum;

topicGroups.js

const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const topicGroupSchema = new Schema({
    title: String,
   // Todo: add topics
})

module.exports = topicGroupSchema;

My test_helper and saveForum_test.js files look like this:

saveForum_test.js

const assert = require("assert");
const Forum = require("../model/forums")

describe("Creating records", () => {
    it("can save a new forum", (done) => {
        const forum = new Forum({
            title: "CodeHUB"
        })
        forum.save()
            .then(() => {
                assert(forum.isNew)
                done();
            })
    })
})

test_helper.js

const mongoose = require("mongoose");
mongoose.Promise = global.Promise;

before(done => {
    mongoose.connect("mongodb://myuser:mypassword@ds221339.mlab.com:21339/clicker", { useNewUrlParser: true });
    mongoose.connection
        .once("open", () => {done()})
        .on("error", error => {
            console.warn("error", error)
        })
})

// FIXME: error when saved twice

beforeEach(done => {
    console.log(mongoose.connection.collections.forums)
    mongoose.connection.dropCollection("forums", () => {
        done();
    })
})

So when I run my test suite with mocha, everything works as expected. But when I change something, and run it again, I get the error

OverwriteModelError: Cannot overwrite forums model once compiled.

I use mlab with mongoose and not a local installed mongodb. Maybe it has something todo with that? I can't figure it out, I checked all the files and imports etc. 10 times and can't find a mistake, can you ?

解决方案

I have solved the problem.

Turns out the problem was how I was running my test suite. My npm run test command in package.json did the following:

"test": "mocha --watch"

Here was the error. I think --watch doesn't reinstantiate everything and is just like "Hot Module Replacement".

I installed nodemon and changed my test script like this:

"test": "nodemon --exec \"mocha -R min\""

This makes sure to rerun the whole files and no error is showing up.

Another related post that should work: mocha --watch and mongoose models

这篇关于为什么我会收到错误“一旦编译就无法覆盖模型"?当我第二次运行测试时在猫鼬?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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