2012-07-27 59 views
0

我有一個簡單的使用案例,我想在嵌入式文檔缺少必填字段的情況下取消保存。Mongoose嵌入式文檔需求檢查/驗證不會冒泡

這裏有一個簡單的例子,一些僞代碼:

var Post = new Schema({ 
    title: { type: String, required: true }, 
    tags: [Tag] 
}) 

var Tag = new Schema({ 
    title: { type: String, required: true } 
}); 

當我保存後,沒有標題,我得到的錯誤。當我用標題和標籤保存帖子,但標籤沒有標題時,我沒有收到任何錯誤,我認爲我應該按照Mongoose文檔:http://mongoosejs.com/docs/embedded-documents.html

以下模型不會產生任何錯誤:

{"title": "new post", "tags": [ {"author":"me"} ] 

我期待它做到了。

+0

'帶有標題和標籤後,但它沒有title'您可以編輯該請部分? Post.title'是否爲空? – 2012-07-27 14:04:05

+0

Post.title不是空的,但Tag.title是,這是一個僞代碼示例,該標籤也應該有一個標題。當我試圖保存這個模型時,它應該給我一個錯誤,但它不會。我編輯了原始問題。 – ragulka 2012-07-27 14:48:08

+1

您可以嘗試在'Post'模式之前定義'Tag'模式嗎?想法來自:https://github.com/LearnBoost/mongoose/issues/722 – 2012-07-27 14:55:34

回答