簡體   English   中英

創建新記錄,而不是更新現有記錄

[英]creating new record rather than updating existing record

我有一個在后台使用Node進行REST的演示Backbone應用程序。 在其中一個Backbone功能中,我從集合中檢索模型並顯示表單以編輯模型,如下所示

   var toEdit = this.menuItems.get(baz);
    this.editFormView = new EditForm({model: toEdit});
   $('#formdiv2').html(this.editFormView.render().el);

一切正常。 模型以表格形式顯示。 在表單視圖中,我具有設置新模型數據並將其保存在提交表單時的功能,但是,當我單擊“提交”時,它是在創建新記錄,而不是更新出現在表單中的記錄。

您可以從下面的代碼中看到為什么會發生這種情況嗎?

由於它正在創建新記錄,因此將在節點后台調用app.post方法,但它應該是app.put方法。

app.post('/sentences', function (req, res){

    var wine = req.body;
    console.log('Adding wine: ' + JSON.stringify(wine));
    db.collection('english', function(err, collection) {
        collection.insert(wine, {safe:true}, function(err, result) {
            if (err) {
                res.send({'error':'An error has occurred'});
            } else {
                console.log('Success: ' + JSON.stringify(result[0]));
                res.send(result[0]);
            }
        });
    });

})

app.put('/sentences/:id', function(req, res){

    var id = req.params.id;
    var wine = req.body;
    delete wine._id;
    console.log('Updating wine: ' + id);
    console.log(JSON.stringify(wine));
    db.collection('english', function(err, collection) {
        collection.update({'_id':new BSON.ObjectID(id)}, wine, {safe:true}, function(err, result) {
            if (err) {
                console.log('Error updating wine: ' + err);
                res.send({'error':'An error has occurred'});
            } else {
                console.log('' + result + ' document(s) updated');
                res.send(wine);
            }
        });
    });
})

這是骨干模型和集合

var MenuItem = Backbone.Model.extend({

     idAttribute: "_id",
     // idAttribute: "question",

    urlRoot: '/sentences'

});


var MenuItems = Backbone.Collection.extend({
    comparator: 'question',
    model: MenuItem,
    url: '/sentences'
});

這些是表單視圖中的保存和設置數據方法。

save: function () {

    this.setModelData();

    this.model.save(this.model.attributes, 
        {
            success: function (model) {
                console.log(model);
                // app.views.pippa.menuItems.add(model);
                // app.navigate('menu-items/' + model.get('url'), {trigger: true});
            }
        }
    );
},

    setModelData: function  () {
        console.log(this.model);
        this.model.set({
            name: this.$el.find('input[name="name"]').val(),
            category: this.$el.find('input[name="category"]').val(),
            _id: null,
            url: this.$el.find('input[name="url"]').val(),
            imagepath: this.$el.find('input[name="imagepath"]').val(),
            uk: this.$el.find('input[name="uk"]').val(),

        });
    }

問題是_id設置為null。 只需取出那條線,它就會起作用

 setModelData: function  () {
        console.log(this.model);
        this.model.set({
            name: this.$el.find('input[name="name"]').val(),
            category: this.$el.find('input[name="category"]').val(),
            _id: null,
            url: this.$el.find('input[name="url"]').val(),
            imagepath: this.$el.find('input[name="imagepath"]').val(),
            uk: this.$el.find('input[name="uk"]').val(),

        });
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM