2015-06-04 86 views
1

我送這個JSON與Angular.js我的node.js/express.js服務:角不在對象發送對象

{ 
    "name": "MyGuitar", 
    "type": "electric", 
    "userid": "123", 
    "likes": 0, 
    "dislike": 0, 
    "guitarParts": { 
"body": { 
    "material": "/content/img/hout.jpg", 
    "_id": "5566d6af274e63cf4f790858", 
    "color": "#921d1d" 
}, 
"head": { 

}, 
"neck": { 
    "material": "/content/img/hout.jpg", 
    "_id": "556d9beed90b983527c684be", 
    "color": "#46921d" 
}, 
"frets": { 

}, 
"pickup": { 

}, 
"bridge": { 

}, 
"buttons": { 

} 

}}

的guitarParts不會保存在MongoDB數據庫中。 貓鼬插入以下:

貓鼬:guitars.insert({名稱: 'MyGuitar',類型: '電動',用戶ID: '123',喜歡:0,_id:的ObjectId( 「557023af9b321b541d4d416e」),guitarParts: [],__v:0})

這是我的貓鼬模型:

guitarPart = new Schema({ 
    id: { type: String, required: true }, 
    color: { type: String, required: true }, 
    material: { type: String, required: true }, 
    x: { type: Number, required: false }, 
    y: { type: Number, required: false }, 
    width: { type: Number, required: false }, 
    height: { type: Number, required: false}, 
}); 

guitarParts = new Schema({ 
    body: [guitarPart], 
    neck: [guitarPart], 
    head: [guitarPart], 
    bridge: [guitarPart], 
    frets: [guitarPart], 
    pickup: [guitarPart], 
    buttons: [guitarPart] 
}); 

guitar = new Schema({ 
    name: { type: String, required: true, unique: false }, 
    type: { type: String, required: true }, 
    userid: { type: String }, 
    likes: { type: Number }, 
    dislikes: { type: Number }, 
    guitarParts: [guitarParts], 
    kidsguitar: { type: Boolean }, 
    lefthanded: { type: Boolean }, 
    assemblykit: { type: Boolean } 
}, 
{ 
    collection: 'guitars' 
}); 

我不知道發生了什麼錯誤。有任何想法嗎?

+0

錯字'guitarparts' - >'guitarParts'? – Pavlo

+0

感謝您的回答。我嘗試過,但仍然無法正常工作。 – Koen

回答

0

根據子文檔的貓鼬文件說:子文檔是與是父母文檔陣列

的元素和架構中的你提供了自己的模式文檔:guitarParts不是數組,它是一個對象,而guitarPart不是數組,它也是一個對象。所以這就是爲什麼它沒有保存。

所以你的架構模型正確的方法將是:

var guitarDefinition = { 
    _id: {type: String, required: true}, 
    color: {type: String, required: true}, 
    material: {type: String, required: true}, 
    x: Number, 
    y: Number, 
    width: Number, 
    heigth: Number 
}; 

var guitarSchema = Schema({ 
    name: { type: String, required: true, unique: false }, 
    type: { type: String, required: true }, 
    userid: String, 
    likes: Number, 
    dislikes: Number , 
    kidsguitar: Boolean, 
    lefthanded: Boolean , 
    assemblykit: Boolean, 

    guitarParts: { 
    body: guitarDefinition, 
    neck: guitarDefinition, 
    head: guitarDefinition, 
    bridge: guitarDefinition, 
    frets: guitarDefinition, 
    pickup: guitarDefinition, 
    buttons: guitarDefinition 
    } 
}); 

其實我已經在我的電腦上運行,它是保存你可以看到我在這裏完整代碼:https://gist.github.com/wilsonbalderrama/f10c38f9fb510865edc2

+0

這工作。謝謝Wilson。 – Koen

+0

不用客氣@KoenBarmentlo – Wilson