Reputation: 33
I need to create references to 2 models which are in different files. My models are:
Profesor
Curso
I want Curso
to have a reference to a Profesor
. My problem is that when the models Curso
is being created, the model Profesor
does not exist yet.
MissingSchemaError Schema hasn't been registered for model Profesor
If the reference is to the model Alumno
there isn´t problem because Alumno
is created before Curso
right?
Upvotes: 0
Views: 670
Reputation: 3141
Just create all your schemas first and then register your models. And do all of this when you bootstrap your app, before anything else. Then you won't have such issues. An example from the docs, where two schema reference each other:
http://mongoosejs.com/docs/populate.html
var mongoose = require('mongoose')
, Schema = mongoose.Schema
var personSchema = Schema({
_id : Number,
name : String,
age : Number,
stories : [{ type: Schema.Types.ObjectId, ref: 'Story' }]
});
var storySchema = Schema({
_creator : { type: Number, ref: 'Person' },
title : String,
fans : [{ type: Number, ref: 'Person' }]
});
var Story = mongoose.model('Story', storySchema);
var Person = mongoose.model('Person', personSchema);
Upvotes: 2