Mongoose Schema失败了

时间:2016-12-09 20:12:08

标签: mongoose-schema

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

以下代码是我正在为Node JS,Express和MongoDB上的Coursera课程编写的模式。我在架构的标签部分收到验证错误,并且在发布时没有显示价格和图像。有没有理由,这是我发布的数据。

{
    "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"
}

对于确定可能原因的任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:1)

标签必须具有值,因为您将其标记为架构中的必填字段。 如果您不想在标签中输入任何值,则从中删除所需的值,并且在发布数据时不会将其作为键传递。

您的架构可以是:

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

放置这样的数据:

{
    "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"
}