Search code examples
ember.jsember-dataember-cli

Ember Data "modelFor" error when serializing models


I am using ember 1.13, ember-cli and ember data 1.19.1 and I am experiencing an error that is difficult to track down due to being unspecific. The error occurs when I do the following:

this.get('model').forEach(function(mymodel) {
    applications.push(elavonApplication.serialize({includeId: true}));
});

where "elavonApplication" is an instance of a DS.Model called "elavon-application" and a snippet of the error itself is:

Uncaught TypeError: Cannot read property 'modelFor' of undefined
ember$data$lib$system$model$$default.reopenClass.typeForRelationship @
ext.js:171ember$data$lib$system$model$$default.reopenClass._findInverseFor @     
ext.js:214ember$data$lib$system$model$$default.reopenClass.inverseFor @ 
ext.js:205Ember.Mixin.create.removeEmbeddedForeignKey @ embedded-records-mixin.js:317
Ember.Mixin.create.serializeBelongsTo @ embedded-records-mixin.js:199

The elavon-application inherits from a parent DS.Model called iso-application. The models are below:

// models/iso_application.js
import DS from 'ember-data';

export default DS.Model.extend({
    status: DS.belongsTo('iso-application-status'),
    statusId: DS.attr('number'),
    quickQuote: DS.belongsTo('quick-quote'),
    quickQuoteId: DS.attr('number'),
    acquirer: DS.belongsTo('acquirer'),
    acquirerId: DS.attr('number'),
    cardPresentStatus: DS.belongsTo('iso-application-status'),
    cardPresentStatusId: DS.attr('number'),
    ecommStatus: DS.belongsTo('iso-application-status'),
    ecommStatusId: DS.attr('number'),
});

// models/elavon-application.js
import DS from 'ember-data';
import IsoApplication from 'paya-operations/models/iso-application';

export default IsoApplication.extend({  
    principal: DS.belongsTo('iso-elavon-principal'),
    businessInfo: DS.belongsTo('iso-elavon-business-info'),
});

The iso-application model has several relationships including one called "status", which links to the "iso-application-status" DS.Model below:

// models/iso-application-status.js
import DS from 'ember-data';

export default DS.Model.extend({
    isoApplications: DS.hasMany('iso-application', {inverse: 'status'}),
    description: DS.attr('string'),
    colour: DS.attr('string')
});

The error only occurs when the relationships are present. After putting in breakpoints and stepping through the code as it runs I have found that in the "typeForRelationship" function in ext.js where Ember is throwing the error the "store" parameter is empty when the error occurs. As I stepped through it was provided for each relationship I have in my models and then when it reached the "status" relationship for it became null. I don't know why this is but it isn't to do with the data supplied to the model as all of the status ids are present in the database.

I use the ActiveModelSerializer and the EmbeddedRecordsMixin to access my server backend. I don't have a serializer for the iso-application-status model or the iso-application model. All of the the properties of iso-application are inherited by the elavon-application model so I have a serializer for that instead:

// serializers/elavon-application.js
import DS from "ember-data";

export default DS.ActiveModelSerializer.extend(DS.EmbeddedRecordsMixin, {
    attrs: {        
        status: {embedded: 'always'},
        quickQuote: {embedded: 'always'},
        acquirer: {embedded: 'always'},
        principal: {embedded: 'always'},
        businessInfo: {embedded: 'always'},
    }
});

Does anyone know what I have done wrong please?

Thanks.


Solution

  • I have managed to fix the problem by re-installing ember-cli and ember-data. I assume something went awry during the upgrade process recently when I went from version 1.12.0 to 1.13.0 and ember-data 1.0.0-beta.17.0 to 1.0.0-beta.19.1.