为什么会出现错误“编译后无法覆盖模型"?当我第二次进行测试时,在猫鼬中? [英] Why do I get error "Cannot overwrite model once compiled" in Mongoose when I run my test a second time?
问题描述
我已阅读相关文章:一旦编译过猫鼬,就无法覆盖模型
这些解决方案都不能解决我的问题.
Problem is neither of these solution's helped me with my problem.
我得到标题错误,并且进行了以下设置:
I get the error in the title and I have following setup:
文件夹结构:
我的模型如下:
forums.js
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
topicGroups.js
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const topicGroupSchema = new Schema({
title: String,
// Todo: add topics
})
module.exports = topicGroupSchema;
我的test_helper和saveForum_test.js文件如下:
My test_helper and saveForum_test.js files look like this:
saveForum_test.js
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
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();
})
})
因此,当我使用Mocha运行测试套件时,一切都会按预期进行. 但是,当我更改某些内容并再次运行时,出现错误
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:编译后无法覆盖
forums
模型.
我将mlab与mongoose一起使用,而不是在本地安装的mongodb.也许与此有关吗?我想不通,我检查了所有文件和导入文件等10次,找不到错误,可以吗?
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.
原来的问题是我如何运行测试套件.
我在package.json
中的npm run test
命令执行了以下操作:
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"
这是错误.我认为--watch
不能重新实例化所有内容,就像热模块更换"一样.
Here was the error. I think --watch
doesn't reinstantiate everything and is just like "Hot Module Replacement".
我安装了nodemon
并更改了测试脚本,如下所示:
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.
另一个应该起作用的相关帖子: mocha --watch和mongoose模型
Another related post that should work: mocha --watch and mongoose models
这篇关于为什么会出现错误“编译后无法覆盖模型"?当我第二次进行测试时,在猫鼬中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!