I have FlashcardSchemas and PackageSchemas in my design. One flashcard can belong to different packages and a package can contain different flashcards.
Below you can see a stripped down version of my mongoose schema definitions:
// package-schema.js
var Schema = mongoose.Schema,
ObjectId = Schema.ObjectId;
var PackageSchema = new Schema({
id : ObjectId,
title : { type: String, required: true },
flashcards : [ FlashcardSchema ]
});
var exports = module.exports = mongoose.model('Package', PackageSchema);
// flashcard-schema.js
var Schema = mongoose.Schema,
ObjectId = Schema.ObjectId;
var FlashcardSchema = new Schema({
id : ObjectId,
type : { type: String, default: '' },
story : { type: String, default: '' },
packages : [ PackageSchema ]
});
var exports = module.exports = mongoose.model('Flashcard', FlashcardSchema);
As you can see from the comments above, these two schema definitions belong to separate files and reference each other.
I get an exception stating that PackageSchema is not defined, as expected. How can I map a many-to-many relation with mongoose?