2014-01-29 54 views
8

我正在開發一個使用mongoose ODM的mongodb節點應用程序。鍵入引用駐留在不同文件中的模式時出現錯誤。類型引用貓鼬模式中的錯誤

我已經在下列文件user.js的代碼:

var mongoose = require('mongoose'); 
var Trip = require('./trip'); 

var userSchema = mongoose.Schema({ 
    firstName: String, 
    lastName: String, 
    salt: String, 
    hash: String, 
    emailAddress: { 
     type: String, 
     unique: true 
    }, 
    trips: [{Type: mongoose.Schema.Types.ObjectId, ref: 'Trip'}] 
}); 

module.exports = mongoose.model('User', userSchema); 

userSchema具有類型參考tripSchema。在我trip.js文件

代碼是:

var tripSchema = mongoose.Schema({ 
     name: String, 
     location: String, 
     arrivalDate: Date, 
     departureDate: Date, 
     type: String}); 

    module.exports = mongoose.model('Trip', tripSchema); 

當我運行應用程序,我收到以下錯誤:

/usr/lib/node_modules/mongoose/lib/schema.js:360 
    throw new TypeError('Undefined type at `' + path + 
     ^
TypeError: Undefined type at `trip.ref` 
    Did you try nesting Schemas? You can only nest using refs or arrays. 
    at Function.Schema.interpretAsType  (/usr/lib/node_modules/mongoose/lib/schema.js:360:11) 
    at Schema.path (/usr/lib/node_modules/mongoose/lib/schema.js:303:29) 
    at Schema.add (/usr/lib/node_modules/mongoose/lib/schema.js:217:12) 
    at Schema.add (/usr/lib/node_modules/mongoose/lib/schema.js:212:14) 
    at new Schema (/usr/lib/node_modules/mongoose/lib/schema.js:73:10) 
    at Function.Schema.interpretAsType (/usr/lib/node_modules/mongoose/lib/schema.js:345:44) 
    at Schema.path (/usr/lib/node_modules/mongoose/lib/schema.js:303:29) 
    at Schema.add (/usr/lib/node_modules/mongoose/lib/schema.js:217:12) 
    at new Schema (/usr/lib/node_modules/mongoose/lib/schema.js:73:10) 
    at Mongoose.Schema (/usr/lib/node_modules/mongoose/lib/schema.js:53:12) 

我無法找出錯誤的原因。如果兩個模式都在同一個文件中,代碼運行良好。但是當我在兩個不同的文件中分離模式時,我遇到了上述錯誤。我該如何解決這個錯誤?任何幫助將不勝感激。

+0

我收到了同樣的錯誤,甚至強硬。我有檢查的回答您的問題不是我案件。一旦我找到解決方案,我會在這裏發佈它,讓其他人在他們到達此頁面時找到解決方案。 – randseed1724

回答

16

您在userSchema中有一個錯字。你已經把

trips: [{Type: mongoose.Schema.Types.ObjectId, ref: 'Trip'}] 

但應

trips: [{type: mongoose.Schema.Types.ObjectId, ref: 'Trip'}] 

Type應該是小寫type

+1

謝謝大家指出! :)我浪費了整整一天的時間,試圖找出錯誤的原因。 – Raeesaa