2016-12-09 17 views
0
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 
}); 

次のコードは、ノード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

ラベルには、スキーマの必須フィールドとしてマークする必要があります。 labelに値を入力しない場合は、必須の値を削除し、データをポストする際にキーとして渡しません。

あなたのスキーマを指定できます。このような

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" 
} 
+0

は実際に、私はそれだけでラベルフィールドのデフォルト値のための単一引用符の代わりに二重引用符にする必要が分かりました。助けてくれてありがとう。 – tcoulson

関連する問題