0

我有这个:

模型/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
}

模型/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
}

模型/国家.js

var mongoose = require("mongoose");
var CountrySchema = new mongoose.Schema({
   name: String,
   areaCode: Number
});
var Country = mongoose.model('Country', CountrySchema);
module.exports = {
   Country: Country
}

它实际上显示了这个错误:

TypeError: Undefined type Modelatcountry 你尝试嵌套模式吗?您只能使用 refs 或数组进行嵌套。

我正在尝试创建一个模型,其中少数类型是另一个模型。如何存档?

4

1 回答 1

4

这里的问题是您正在从 country.js 导出模型并通过在地址架构创建中的要求来使用相同的模型。在创建嵌套模式时,属性的值应该是模式对象而不是模型。

将您的 country.js 更改为:

var mongoose = require("mongoose");
var CountrySchema = new mongoose.Schema({
   name: String,
   areaCode: Number
});
module.exports = {
   Country: CountrySchema
}
于 2018-02-05T15:24:47.267 回答