Mongoose验证错误:架构配置无效

uqxowvwt  于 2023-05-13  发布在  Go
关注(0)|答案(2)|浏览(127)

我在mongoose中创建了一个Schema,当我启动node时,它崩溃了

const mongoose = require("mongoose");
const { DataView } = require("webidl-conversions");

const productSchema = new mongoose.Schema({
    name:{
        type:String,
        required:[true,"Please Enter Product Name"],
        trim:true
    },
    description:{
        type:String,
        required:[true,"Please Enter Product Description"]
    },
    price:{
        type:Number,
        required:[true,"Please Enter Product Price"],
        maxLength:[6,"Price cannot exceed 6 characters"]
    },
    rating:{
        type:Number,
        default:0
    },
    images:[
        {
            public_id:{
                type:String,
                required:true,
            },
            url:{
                type:String,
                required:true,
            }
        }
    ],
    category:{
        category:String,
        required:[true,"Please Enter Product Category"],
    },
    stock:{
        type:Number,
        required:[true,"Please Enter Product Stock"],
        maxLength:[4,"stock cannot excee 4 characters"],
        default:1
    },
    numOfReviews:{
        type:Number,
        default:0
    },
    reviews:[
        {
            name:{
                type:String,
                required:true,
            },
            rating:{
                type:Number,
                required:true,
            },
            comment:{
                type:String,
                required:true,
            }

        }
    ],
    createdAt:{
        type:Date,
        default:Date.now
    }
})

mosule.export = mongoose.model("Product",productSchema);

这是我的schema代码,我得到TypeError:无效的架构配置:无法确定数组category.required的嵌入类型。请参阅https://mongoosejs.com/docs/guide.html#definition了解更多关于支持的模式语法的信息此错误我不知道是什么问题,所以请帮助我,提前感谢

irlmq6kh

irlmq6kh1#

基于我在这里看到的,似乎你导出模块的方式是一个给你的问题。
module.exports改为mosule.export
快看

qyswt5oh

qyswt5oh2#

category:{
        category:String,
        required:[true,"Please Enter Product Category"],
    }

如果你想要一个类别数组,那么你可以用这个

category:{
        type: Array,
        required:[true,"Please Enter Product Category"],
    }

相关问题