2013-02-01 40 views
15

我有一個更新MongoDB中任何集合的文檔的常用方法?如何擺脫錯誤:「OverwriteModelError:一旦編譯就無法覆蓋`undefined`模型。」?

下面的代碼是在文件名Deleter.js

module.exports.MongooseDelete = function (schemaObj, ModelObject); 
{ 
    var ModelObj = new mongoose.Model("collectionName",schemaObj); 
    ModelObj.remove(ModelObject); 
} 

並調用在我的主文件如下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) 

我只有第二個方法調用.. 請讓我知道是否有任何人有一些解決方案。

+1

什麼是你想用'MongooseDelete'辦?爲什麼不直接調用'Model_instance.remove();'? – JohnnyHK

+0

我只是試圖使刪除調用一個常用的方法,在我的自定義模塊..即使我調用MongooseDelete(/ * params */n次提供如果架構是相同的,我的工作原理,當我嘗試刪除不同的文檔架構,我收到以上錯誤消息。感謝您對回答的好感... –

+1

難道你不能改變'MongooseDelete'來調用'ModelObject.remove();'? – JohnnyHK

回答

21

我想你已經在同一個模式上實例化了兩次mongoose.Model()。你應該已經創建的每個模型只有一次,有一個全局對象需要

,當我想你下的目錄申報在不同的文件不同型號$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(..... 
+0

這是迄今爲止更好的解決方案。 –

8

我會盡量避免全局儘可能的,因爲一切都是通過引用,任何事情都變得凌亂。我的解決方案

model.js

try { 
    if (mongoose.model('collectionName')) return mongoose.model('collectionName'); 
    } catch(e) { 
    if (e.name === 'MissingSchemaError') { 
     var schema = new mongoose.Schema({ name: 'abc }); 
     return mongoose.model('collectionName', schema); 
    } 
    } 
2

我發現它更好地避免全球和異常handing-

var mongoose = require("mongoose"); 
var _ = require("underscore"); 

var model; 
if (_.indexOf(mongoose.modelNames(), "Find")) { 
    var CategorySchema = new mongoose.Schema({ 
     name: String, 
     subCategory: [ 
      { 
       categoryCode: String, 
       subCategoryName: String, 
       code: String 
      } 
     ] 
    }, { 
     collection: 'category' 
    }); 
    model = mongoose.model('Category', CategorySchema); 
} 
else { 
    model = mongoose.model('Category'); 
} 


module.exports = model; 
+0

不是指_ _indexOf(mongoose.modelNames(),「Category」)? – Lior

+0

和@Kavi,did not u mean _.indexOf(mongoose.modelNames(),「Category」)> 0? – Lior

+0

_.indexOf(mongoose.modelNames(),「Category」)<0 – Kavi

37

我設法解決這樣的問題:

var Admin; 

if (mongoose.models.Admin) { 
    Admin = mongoose.model('Admin'); 
} else { 
    Admin = mongoose.model('Admin', adminSchema); 
} 

module.exports = Admin; 
+0

謝謝。我用這個解決方案修復了TypeScript中的bug。 –

+0

你能解釋一下它是如何工作的嗎?非常感謝:) –

2

其實問題不是mongoose.model()被實例化了兩次。 問題是Schema被實例化了多次。 例如,如果您n次執行mongoose.model("Model", modelSchema),並且您對模式使用相同的引用,這對貓鼬來說不會造成問題。 當您在同一模型i上使用另一個架構參考時,會出現問題。e

var schema1 = new mongoose.Schema(...); 
mongoose.model("Model", schema1); 
mongoose.model("Model", schema2); 

這是發生此錯誤時的情況。

如果你看一下源(mongoose/lib/index.js:360)這是檢查

if (schema && schema.instanceOfSchema && schema !== this.models[name].schema){ 
    throw new mongoose.Error.OverwriteModelError(name); 
} 
+0

很好的答案,謝謝!因此,可以繞過對模式的調用,檢查mongoose.models [name]是否已包含該項目 –

相關問題