[英]Mongoose typeError with custom Schema
我正在嘗試使用discountSchema
作為類型。 但我收到此錯誤:
throw new TypeError('Undefined type at `' + path +
^
TypeError: Undefined type at `discount`
但是如果我轉換為array
類型:
discount: {
type: [discountSchema],
default:{}
}
有用。
我怎樣才能在貓鼬中使用復雜類型呢? 我是否以錯誤的方式使用此模型? 我如何像這樣建模這個對象?
var discountSchema = new Schema({
type: {type: String,default:'' },
quantity: {type: Number,default:0 },
value: {type: Number,default:0 }
});
var objectEncomendaPropertiesSchema = {
recheios:{
type:[String],
default: [],
select: true
},
availableEncomenda: {
type: Boolean,
default:false
},
discount: {
type: discountSchema,
default:{}
}
};
您是否要創建兩個模式然后將它們連接起來?
var discountSchema = new Schema({
type: {type: String,default:'' },
quantity: {type: Number,default:0 },
value: {type: Number,default:0 }
});
mongoose.model('Discount', discountSchema);
var objectEncomendaPropertiesSchema = new Schema({
recheios:{
type: String,
default: '',
select: true
},
availableEncomenda: {
type: Boolean,
default:false
},
discount: {
type: Schema.Types.ObjectId,
ref: 'Discount'
}
})
mongoose.model('objectEncomendaProperties', objectEncomendaPropertiesSchema);
我引用第二個模式中的折扣以通過ObjectId引用第一個模式
它將獲取Discount模型中的屬性作為第二個Schema中的Discount屬性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.