Itu karena apa yang Anda ekspor di modul Anda bukan Schemas
, mereka adalah Models
. Ketika Anda melakukan var EventSchema = require('../Models/Event');
Anda membutuhkan Event Model
, bukan Event Schema
. Untuk mengakses skema dasar dari model Anda, Anda dapat melakukan:
var EventSchema = require('../Models/Event').schema;
var InterestSchema = require('../Models/Interest').schema;
var UserSchema = require('../Models/User').schema;
Anda juga memiliki masalah saat Anda mereferensikan dokumen milik koleksi lain, ini akan berhasil:
Model Peristiwa:
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);
Model Minat:
var mongoose = require('mongoose');
var InterestSchema = new mongoose.Schema({
name: String
});
module.exports = mongoose.model('Interest', InterestSchema);
Model Pengguna:
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);
Cara mereferensikan skema lain di Skema luwak? http://mongoosejs.com/docs/populate.html