mercredi 28 octobre 2015

Error for one hasMany but not another. Makes no sense

I have a model "Group" that has some hasMany relationships "members" and "links". I have a controller that adds a new group but i get the following error on save

"Assertion Failed: A App.Group record was pushed into the store with the value of links being '{}', but links is a hasMany relationship so the value must be an array. You should probably check your data payload or serializer."

I can fix this by returning an empty links array in the response json but i don't necessarily want this included and i don't understand why i get this error for links but not for members as that isn't included in the response either.

App.AddGroupController = Ember.ObjectController.extend({
    needs: ['application'],
    actions: {
        submitForm: function(){
            var self = this;
            var model = this.get('model');
            self.send('showLoading', 'Saving');
            model.save().then(function(){
                self.send('hideLoading');
                //Go to the groups list
                self.transitionToRoute('groups');
            },function(error){
                self.send('hideLoading');
                console.log(error);
                //Must supply reject callback, otherwise Ember will throw a 'backend rejected the commit' error.
            });
        }
    }
});

App.AddGroupRoute = App.AuthenticatedRoute.extend({
    model: function(){
        return this.store.createRecord('group');
    }
});

App.Group = DS.Model.extend({
    title: DS.attr('string'),
    description: DS.attr('string'),
    members: DS.hasMany('groupMember'),
    links: DS.hasMany('groupLink'),
});

App.GroupLink = DS.Model.extend({
    title: DS.attr('string'),
    link: DS.attr('string'),
});

App.GroupMember = DS.Model.extend({
    first_name:      DS.attr('string'),
    last_name:       DS.attr('string'),
    email:           DS.attr('string'),
    profile_picture: DS.attr('string'),
});

Response from save:

{
   "group":{
      "id":26,
      "title":"Test Group",
      "description":"Dummy group description",
   }
}

Im using:
Ember : 1.9.1
Ember Data : 1.13.14




Aucun commentaire:

Enregistrer un commentaire