2

i make a Schema in mongoose and when i start node it crash

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);

this is my schema code and i get TypeError: Invalid schema configuration: Could not determine the embedded type for array category.required. See https://mongoosejs.com/docs/guide.html#definition for more info on supported schema syntaxes this error i don't know what is wrong so please help me, Thanks in Advance

2 Answers 2

1

based on what am seeing here, it seems the ways you are exporting your module is the one giving you problem.

Instead of module.exports, you wrote mosule.export

Check it out

Sign up to request clarification or add additional context in comments.

Comments

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

if you want an array of catagory then you can use this

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.