问题描述
我已阅读相关帖子:
我的模型如下所示:
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:[email protected]: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:
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;
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:
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();
})
})
})
const mongoose = require("mongoose");
mongoose.Promise = global.Promise;
before(done => {
mongoose.connect("mongodb://myuser:[email protected]: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
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
这篇关于为什么我会收到错误“一旦编译就无法覆盖模型"?当我第二次运行测试时在猫鼬?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!