我想将类别架构嵌入到产品架构中,该类别架构实际上只是对象ID和标题。
我的类别模型:
const categorySchema = new mongoose.Schema({
name: {
type: String,
required: true,
minlength: 5,
maxlength: 255
}
});
const Category = mongoose.model("Category", categorySchema);
module.exports.categorySchema = categorySchema;
module.exports = Category;
产品型号:
const { categorySchema } = require("./categoryModel");
const productSchema = new mongoose.Schema({
name: {
type: String,
required: true,
minlength: 5,
maxlenth: 255
},
description: {
type: String,
required: true,
minlength: 5,
maxlength: 255
},
price: {
type: Number,
required: true,
min: 0
},
category: {
type: categorySchema,
required: true
},
});
const Product = mongoose.model("Product", productSchema);
module.exports = Product;
我在控制台中遇到的错误是:
TypeError模式路径
category.type
的无效值
如果我用console.log()退出categorySchema,则会得到以下信息:
Schema {
obj: {
name: {
type: [Function: String],
required: true,
minlength: 5,
maxlength: 255
}
},
paths: {
name: SchemaString {
enumValues: [],
regExp: null,
path: 'name',
instance: 'String',
validators: [Array],
getters: [],
setters: [],
options: [Object],
_index: null,
isRequired: true,
requiredValidator: [Function],
originalRequiredValue: true,
minlengthValidator: [Function],
maxlengthValidator: [Function],
[Symbol(mongoose#schemaType)]: true
},
答案 0 :(得分:1)
代替
category: {
type: categorySchema,
required: true
}
使用
category : categorySchema
此外,您的导出似乎有错误。
尝试将其更改为此:
module.exports = {
Category : Category,
categorySchema : categorySchema
}