MongoDB是MongoDB的一個ODM,我有一個有趣的問題。Mongoose Model別名錯誤
我想將mongoose.model
方法別名爲Model
。我甚至檢查別名:
exports = Model = mongoose.model;
console.log(Model === mongoose.model); // returns true
我已經做了這個mongoose.Schema
,它的工作無縫。
現在,當我註冊使用別名Model
變量的模式:
Model('User', UserSchema);
我收到以下錯誤:
/node_modules/mongoose/lib/index.js:257
if (!this.modelSchemas[name]) {
^
TypeError: Cannot read property 'User' of undefined
at Mongoose.model (/node_modules/mongoose/lib/index.js:257:25)
at Object.<anonymous> (/app/models/user.js:20:1)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at controllers_path (/app.js:23:2)
at Array.forEach (native)
但是,如果使用正常的形式,我得到絕對沒有錯誤:
mongoose.model('User', UserSchema);
- 我這是一個在
Mongoose.js ODM
的錯誤還是我錯過了什麼?
'Model'是Model對象的構造函數;在本地更改爲'mongoose.model'函數似乎充其量讓人感到困惑。您是否嘗試使用'model'的別名? – JohnnyHK
是的,我試過了。即使使用隨機名稱,它也不起作用並返回相同的錯誤。 –