简体   繁体   中英

creating new record rather than updating existing record

I have a demo Backbone app using Node in the background for REST. In one of the Backbone functions, I retrieve a model from a collection and display a form to edit the model like this

   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('/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. 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(),

        });
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM