Reputation: 6796
I've this:
models/record.js
var mongoose = require("mongoose");
var RecordSchema = new mongoose.Schema({
address : require("./address").Address
});
var Record = mongoose.model('Record', RecordSchema);
module.exports = {
Record: Record
}
models/address.js
var mongoose = require("mongoose");
var AddressSchema = new mongoose.Schema(
{
streetLine1: String,
streetLine2: String,
city: String,
stateOrCounty: String,
postCode: String,
country: require("./country").Country
});
var Address = mongoose.model('Address', AddressSchema);
module.exports = {
Address: Address
}
models/country.js
var mongoose = require("mongoose");
var CountrySchema = new mongoose.Schema({
name: String,
areaCode: Number
});
var Country = mongoose.model('Country', CountrySchema);
module.exports = {
Country: Country
}
It's actually showing this error:
TypeError: Undefined type Model
at country
Did you try nesting Schemas? You can only nest using refs or arrays.
I am trying to create a model where few types is another model. How to archive this ?
Upvotes: 0
Views: 1929
Reputation: 495
The problem here is that you are exporting a model from country.js and using the same by requiring in the address schema creation. While creating nested schemas, the value for a property should be a schema Object and not a model.
Change your country.js to:
var mongoose = require("mongoose");
var CountrySchema = new mongoose.Schema({
name: String,
areaCode: Number
});
module.exports = {
Country: CountrySchema
}
Upvotes: 4