A lot of undefined relationship in calculated relationship Unable to read property 'reject' of indefinite

advertisements

Given that I have the following

Model

var Artistecontract = ErrorManaged.extend({
  name: DS.attr('string'),
  email: DS.attr('string'),
  signatoryEmail: DS.attr('string'),
  address: DS.attr('string'),
  engagements: DS.hasMany('engagement', { async: true }),
  artisteproject: DS.belongsTo('artisteproject', { async: true }),
});

Controller

var ArtistecontractEngagementsController = Ember.ObjectController.extend({
  availableEngagements: function () {
    var engagements = this.get('engagements');
    return this.get('artisteproject.engagements').reject(function (engagement) {
      return engagements.contains(engagement)
    });
  }.property('artisteproject.engagements', 'engagements')
});

Route

var ArtistecontractEngagementRoute = AuthenticatedRoute.extend({
  model: function(params) {
    return this.store.find('artistecontract', params.id);
  },
  actions: {
    include: function (artisteproject, engagement) {
      artisteproject.get('engagements').then(function (engagements) {
        engagements.pushObject(engagement);
        artisteproject.save();
      });
    }
  }
});

When I access the availableEngagements attribute I then receive

Cannot read property 'reject' of undefined

What I want to do Is get a array of engagements from the artisteproject that aren't already in the engagements attribute.

What am I doing wrong? Or is there a better way to do this.


You're calling .reject on this.get('artisteproject.engagements'), but the error message is telling you this.get('artisteproject.engagements') is undefined, meaning either

  1. this.get('artisteproject') is undefined, or
  2. this.get('artisteproject.engagements') is undefined.

Based on the code you posted, you never defined an artisteproject property on the controller. In Ember, whatever you return from the model hook of a route gets set as the model property of the associated controller. So, in your controller code, try this instead:

this.get('model.engagements').reject...