私はMongoDBとBackboneの初心者ですので、理解しようとしていますが、難しいです。大きな問題があります。Backboneでモデルの属性を操作する方法を理解できません.Modelを使用して、必要なものだけをビューで使用します。より具体的な - 私はモデルありますMongoError:文書の_idを変更できません
window.User = Backbone.Model.extend({
urlRoot:"/user",
idAttribute: "_id",
defaults: {
_id: null,
name: "",
email: "[email protected]"
}
});
window.UserCollection = Backbone.Collection.extend({
model: User,
url: "user/:id"
});
を私はビューあります
beforeSave: function(){
var self = this;
var check = this.model.validateAll();
if (check.isValid === false) {
utils.displayValidationErrors(check.messages);
return false;
}
this.saveUser();
return false;
},
saveUser: function(){
var self = this;
console.log('before save');
this.model.save(null, {
success: function(model){
self.render();
app.navigate('user/' + model.id, false);
utils.showAlert('Success!', 'User saved successfully', 'alert-success');
},
error: function(){
utils.showAlert('Error', 'An error occurred while trying to save this item', 'alert-error');
}
});
}
を私は「_id」以外の任意のフィールドから「置く」メソッドの聖霊降臨祭のデータを使用する必要があり、そう、それはのようになめらかである必要があります。
{"name": "Foo", "email": "[email protected]"}
しかしたびに、私はそれが
を送って何をすべきかに依存しません{**"_id": "5083e4a7f4c0c4e270000001"**, "name": "Foo", "email": "[email protected]"}
と、サーバーからこのエラー:
MongoError: cannot change _id of a document old:{ _id: ObjectId('5083e4a7f4c0c4e270000001'), name: "Foo" } new:{ _id: "5083e4a7f4c0c4e270000001", name: "Bar", email: "[email protected]" }
Githubのリンク:事前にhttps://github.com/pruntoff/habo
ありがとう!
を見る()' POSTが実行またはPUTされていますか? – InPursuit
が実行されます。 'app.get( '/ user /:id'、usr.findById); app.put( '/ user /:id'、usr.updateUser); ' – Pruntoff
' save'を呼び出すときにバックボーンがPUTを実行している場合、モデルのidAttribute(あなたのケースの_id)は送信されません。あなたのモデルで 'save()'の前に 'isNew()'を呼び出し、 'false'を返すことを確認できますか?また、 'save()'を呼び出す前に 'this.model.id'がnullでないか、または未定義であることを確認してください。 – InPursuit