destroyRecord is not a function

Hello,

I have two models:

campaign:

import DS from 'ember-data';

export default DS.Model.extend({
  name:                 DS.attr('string'),
  address:              DS.belongsTo('address')
});

address:

import DS from 'ember-data';

export default DS.Model.extend({
  campaign: DS.belongsTo('campaign'),
  name:     DS.attr('string'),
  lat:      DS.attr('number'),
  lon:      DS.attr('number')
});

A route campaign_new:

export default Ember.Route.extend({
  model() {
    let campaign = this.store.createRecord('campaign');
    this.store.createRecord('address', {
      campaign: campaign
    });
    return campaign;
  },
  exit() {
    let campaign = this.currentModel;
    let address = campaign.get('address');
    if (address.get('id') == null)
      address.destroyRecord();
    if (campaign.get('id') == null)
      campaign.destroyRecord();
  }
});

But on exit, this error occured:

Error while processing route: app.campaigns address.destroyRecord is not a function TypeError: address.destroyRecord is not a function

I have a similar error on address.save().

(My models and routes are simplified for more readibility).

Anyone has an idea ? :slight_smile:

It could be that the address relationship is async by default.

So:

campaign.get('address').then(function(a) {
  if (a.get('id') === null) {
    a.destroyRecord();
  }
});

You could set the relationship to {async: false}.

Let me know if this helps!

Ben

1 Like

Effectively, i forgot to use the callback notation ( I come from ruby :smile: ) , it works fine.

Thanks a lot !