2017-03-26 40 views
0

我想,我已經創造了其他模式嵌入模式,我不斷收到此錯誤:嵌入模式是給錯誤

我不能完全確定是怎麼回事錯在這裏,但我想要做什麼是存儲用戶架構內的事件架構和興趣架構的引用。如果有人能告訴我我做錯了什麼,那將是非常感謝!

編輯:我現在接收新的錯誤:

/Users/Dynee/node_modules/mongoose/lib/schema.js:421 
    throw new TypeError('Invalid value for schema Array path `' + prefix + key + '`'); 
^

TypeError: Invalid value for schema Array path `eventsHosted` 
at Schema.add (/Users/Dynee/node_modules/mongoose/lib/schema.js:421:13) 
at new Schema (/Users/Dynee/node_modules/mongoose/lib/schema.js:99:10) 
at Object.<anonymous> (/Users/Dynee/Documents/eventure-rest-backend/Models/User.js:5:18) 
at Module._compile (module.js:570:32) 
at Object.Module._extensions..js (module.js:579:10) 
at Module.load (module.js:487:32) 
at tryModuleLoad (module.js:446:12) 
at Function.Module._load (module.js:438:3) 
at Module.require (module.js:497:17) 
at require (internal/module.js:20:19) 
at Object.<anonymous> (/Users/Dynee/Documents/eventure-rest-backend/Models/Event.js:2:43) 
at Module._compile (module.js:570:32) 
at Object.Module._extensions..js (module.js:579:10) 
at Module.load (module.js:487:32) 
at tryModuleLoad (module.js:446:12) 
at Function.Module._load (module.js:438:3) 

我的用戶模式

var mongoose = require('mongoose'); 
var EventSchema = require('../Models/Event').schema; 
var InterestSchema = require('../Models/Interest').schema; 

var UserSchema = new mongoose.Schema({ 
    email: String, 
    password: String, 
    eventsHosted: [EventSchema], 
    eventsAttended: [EventSchema], 
    currentlyAttending: [EventSchema], 
    currentlyHosting: [EventSchema], 
    profileImage: String, 
    interests: [InterestSchema], 
    followers: [UserSchema], 
    following: [UserSchema] 

}); 

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

我的事件模式

var mongoose = require('mongoose'); 
var UserSchema = require('../Models/User').schema; 

var EventSchema = new mongoose.Schema({ 
    title: String, 
    description: String, 
    location: String, 
    attendees: [UserSchema], 
    date: String, 
}); 

module.exports = mongoose.model('Event', EventSchema); 

我的興趣模式

var mongoose = require('mongoose'); 

var InterestSchema = new mongoose.Schema({ 
    name: String 
}); 

module.exports = mongoose.model('Interest', InterestSchema); 
+0

[用於模式陣列路徑無效值(的可能的複製http://stackoverflow.com/questions/30856208/invalid-value-for -schema-array-path) – Veeram

+0

我看着那個線程,它並沒有解決我的問題。 – Dynee

回答

0

,那是因爲你導出你的模塊不Schemas,他們是Models。當你做var EventSchema = require('../Models/Event');你需要Event Model,而不是Event Schema。爲了從模型訪問底層架構,你可以這樣做:

var EventSchema = require('../Models/Event').schema; 
var InterestSchema = require('../Models/Interest').schema; 
var UserSchema = require('../Models/User').schema; 

您也可以當你引用屬於另一個集合文檔的問題,這應該工作:

事件模型:

var mongoose = require('mongoose'); 
var UserSchema = require('./User').schema; 

var EventSchema = new mongoose.Schema({ 
    title: String, 
    description: String, 
    location: String, 
    attendees: [{ type: Schema.Types.ObjectId, ref: 'User' }], 
    date: String 
}); 

module.exports = mongoose.model('Event', EventSchema); 

利息型號:

var mongoose = require('mongoose'); 

var InterestSchema = new mongoose.Schema({ 
    name: String 
}); 

module.exports = mongoose.model('Interest', InterestSchema); 

用戶模型:

var mongoose = require('mongoose'); 
var EventSchema = require('./Event').schema; 
var InterestSchema = require('./Interest').schema; 

var UserSchema = new mongoose.Schema({ 
    email: String, 
    password: String, 
    eventsHosted: [{ type: Schema.Types.ObjectId, ref: 'Event' }], 
    eventsAttended: [{ type: Schema.Types.ObjectId, ref: 'Event' }], 
    currentlyAttending: [{ type: Schema.Types.ObjectId, ref: 'Event' }], 
    currentlyHosting: [{ type: Schema.Types.ObjectId, ref: 'Event' }], 
    profileImage: String, 
    interests: [{ type: Schema.Types.ObjectId, ref: 'Interest' }], 
    followers: [{ type: Schema.Types.ObjectId, ref: 'User' }], 
    following: [{ type: Schema.Types.ObjectId, ref: 'User' }] 
}); 

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

How to reference another schema in my Mongoose schema? http://mongoosejs.com/docs/populate.html

+0

你好!謝謝你的回覆,我將.schema添加到每個導入的結尾,但是,我收到了一個類似的錯誤,如果你能看看我的錯誤,欣賞它。 – Dynee

+0

如果我想引用整個事件,用戶或興趣,該怎麼辦?參加活動,參加等。我想引用整個對象,而不僅僅是一個數字。也許我可以只做Schema.Types.ObjectId,然後我可以訪問我希望它引用的整個Event,User或Interest。 – Dynee

+0

現在已經修復;) – luisenrike