简体   繁体   English

创建新记录,而不是更新现有记录

[英]creating new record rather than updating existing record

I have a demo Backbone app using Node in the background for REST. 我有一个在后台使用Node进行REST的演示Backbone应用程序。 In one of the Backbone functions, I retrieve a model from a collection and display a form to edit the model like this 在其中一个Backbone功能中,我从集合中检索模型并显示表单以编辑模型,如下所示

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

This is working fine. 一切正常。 The model is displaying in the form. 模型以表格形式显示。 In the form view, I have a function to set the new model data and to save it on submission of the form, however, when I click submit, it's creating a new record rather than updating the record that appears in the form. 在表单视图中,我具有设置新模型数据并将其保存在提交表单时的功能,但是,当我单击“提交”时,它是在创建新记录,而不是更新出现在表单中的记录。

Can you see why that's happening from the code below? 您可以从下面的代码中看到为什么会发生这种情况吗?

Since it's creating a new record, the app.post method is being called in the node background, but it should be the app.put method. 由于它正在创建新记录,因此将在节点后台调用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);
            }
        });
    });
})

This is the Backbone model and the collection 这是骨干模型和集合

var MenuItem = Backbone.Model.extend({

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

    urlRoot: '/sentences'

});


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

These are the save and set data methods in the form view. 这些是表单视图中的保存和设置数据方法。

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(),

        });
    }

The problem is the setting of _id to null. 问题是_id设置为null。 Just take out that line and it'll work 只需取出那条线,它就会起作用

 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