2014-09-10 95 views
1

我有貓鼬模型實例集貓鼬模型實例屬性:與「擴展」 UTIL

var s = new Song({ 
    song_id: '123', 
    title: 'sample song', 
    artist: { 
    name: 'sample artist', 
    id: '456' 
    } 
}); 

現在我想設置/更新其特性,但使用extend(例如,從util._extend的NodeJS)

s = extend(s, { 
    title: 'changed title', 
    artist: { 
    name: 'changed artist', 
    id: '789' 
    } 
}); 

s.save(); 

雖然title(作爲頂級屬性)設置正常,但artist中的更改不可見。

我知道我可以通過設置它:

s.artist.name = 'changed artist'; 

但有什麼我失蹤或更新無法使用性質的這種方式?

編輯

爾加...貌似有人定義的模式走錯了路。作爲

artist: { 
    name: String, 
    id: String 
} 

這就像

artist.name: String, 
artist.id: String 

定義。當我重新定義了它現在的工作,而不是artist領域的定義。由於

回答

0

你可以做的就是用貓鼬的更新方法:

Song.update({ 
    song_id: '123', 
}, { 
    $set: { 
     title: 'changed title', 
     artist: { 
      name: 'changed artist', 
      id: '789', 
     }, 
    }, 
}, function(err, numAffected) { 
    ... 
}); 

不過,我真的不明白爲什麼你的擴展嘗試失敗。它似乎爲我工作,我通常使用下劃線的_.extend函數。

+0

Gah ...看起來像某人定義架構錯誤。而不是'artist:{name:String,id:String}'它被定義爲'artist.name:String,artist.id:String'。當我重新定義它時,它現在起作用。謝謝 – 2014-09-10 17:59:02