Search code examples
node.jsmongodbmongoose

Mongoose Required true validation not working


I am trying out nodejs and using mongoose to save a document to mangoDB but my validation for name which I set to required : true is not working. Though I have not set name field the document gets saved. Am i missing an thing in my code

const courseSchema = new mongoose.Schema({
    name: {type:String, requried:true},
    author: String,
    tags: [ String ],
    date: Date,
    isPublished: Boolean,
    price: Number
});
const Course = mongoose.model('Course',courseSchema);

async function createCourse() {
    try {
        const course = new Course({
            author: "Srikanth xyz",
            tags: ['express','js'],
            isPublished: true,
            price: 15
        });
        const result = await course.save();
        console.log(result);    
    } catch (error) {
        console.log("ERROR: " + error.message);
    }

}
createCourse();

Solution

  • You might want to double check "requried" to "required"