tcoulson
tcoulson

Reputation: 624

Mongoose Schema is failing

var dishSchema = new Schema({
    name: {
        type: String,
        required: true,
        unique: true
    },
    image: {
        type: String,
        required: true
    },
    category: {
        type: String,
        required: true
    },
    label: {
        type: String,
        default: "",
        required: true
    },
    price: {
        type: Currency,
        required: true
    },
    description: {
        type: String,
        required: true
    },
    comments:[commentSchema]
}, {
    timestamps: true
});

The following code is my schema I am writing for a Coursera course on Node JS, Express and MongoDB. I am getting a validation error on the label part of the schema, and price and image are not showing up when posted. Is there a reason, here is the data I posted.

{
    "name": "Zucchipakoda",
    "image": "images/zucchipakoda.png",
    "category": "appetizer",
    "label": "",
    "price": "1.99",
    "description": "Deep fried Zucchini coated with mildly spiced Chickpea flour batter accompanied with a sweet-tangy tamarind sauce"
}

Any help in identifying possible reasons for this are appreciated.

Upvotes: 0

Views: 51

Answers (1)

Simran
Simran

Reputation: 2830

Label must have value as you mark it as a required field in your schema. If you do not want to enter any value in label then remove required from it and will not pass it as a key while posting data.

Your schema can be:

var dishSchema = new Schema({
    name: {
        type: String,
        required: true,
        unique: true
    },
    image: {
        type: String,
        required: true
    },
    category: {
        type: String,
        required: true
    },
    label: {
        type: String
    },
    price: {
        type: Currency,
        required: true
    },
    description: {
        type: String,
        required: true
    },
    comments:[commentSchema]
}, {
    timestamps: true
});

Put data like this :

{
    "name": "Zucchipakoda",
    "image": "images/zucchipakoda.png",
    "category": "appetizer"
    "price": "1.99",
    "description": "Deep fried Zucchini coated with mildly spiced Chickpea flour batter accompanied with a sweet-tangy tamarind sauce"
}

Upvotes: 2

Related Questions