2013-12-12 41 views
0

有沒有比使用JSON.parse更好的方法?將字段設置爲帶貓鼬的字符串

userSchema.methods.incrementPlaylist = function(playlist, value) { 
    return this.update(JSON.parse("{\"$inc\": {\"playlists." + playlist + "\": " + value + "}}")); 
}; 

回答

0

是,建立你的更新對象是這樣,而不是:

userSchema.methods.incrementPlaylist = function(playlist, value) { 
    var update = { $inc: {} }; 
    update.$inc['playlists.' + playlist] = value; 
    return this.update(update); 
}; 
+0

謝謝。我不知道爲什麼我被對象文字卡住了,我知道我錯過了一些東西...... – standup75