如何摆脱错误:"OverwriteModelError:编译后无法覆盖未定义的模型"? [英] How to get rid of Error: "OverwriteModelError: Cannot overwrite `undefined` model once compiled."?
本文介绍了如何摆脱错误:"OverwriteModelError:编译后无法覆盖未定义的模型"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一种通用的方法来更新MongoDB中任何集合的文档吗?
I have a common method for updating document of any collection in MongoDB?
以下代码位于文件名 Deleter.js
module.exports.MongooseDelete = function (schemaObj, ModelObject);
{
var ModelObj = new mongoose.Model("collectionName",schemaObj);
ModelObj.remove(ModelObject);
}
并在我的主文件 app.js 中调用如下:
And invoking as follows in my main file app.js:
var ModObj = mongoose.model("schemaName", schemasObj);
var Model_instance = new ModObj();
var deleter = require('Deleter.js');
deleter.MongooseDelete(schemasObj,Model_instance);
我遇到以下错误:
OverwriteModelError: Cannot overwrite `undefined` model once compiled.
at Mongoose.model (D:\Projects\MyPrjct\node_modules\mongoose\lib\index.js:4:13)
我只能进行第二种方法调用. 请让我知道是否有人可以解决.
I get on 2nd method call only.. Please let me know if any one has got some solution.
推荐答案
我认为您已在同一架构上实例化mongoose.Model()
两次.您应该只创建一次每个模型,并且需要一个全局对象来在需要时保留它们
I think you have instantiated mongoose.Model()
on the same schema twice. You should have created each model only once and have a global object to get a hold of them when need
我假设您在目录$YOURAPP/models/
$YOURAPPDIR/models/
- index.js
- A.js
- B.js
index.js
module.exports = function(includeFile){
return require('./'+includeFile);
};
A.js
module.exports = mongoose.model('A', ASchema);
B.js
module.exports = mongoose.model('B', BSchema);
在您的app.js中
APP.models = require('./models'); // a global object
在需要时
// Use A
var A = APP.models('A');
// A.find(.....
// Use B
var B = APP.models('B');
// B.find(.....
这篇关于如何摆脱错误:"OverwriteModelError:编译后无法覆盖未定义的模型"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文