1
votes

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.

1
What response does the server provide? - jmurphyau
You say all status IDs are present.. You've specified status as an embedded record - meaning the data should be expecting the full object, not just an ID that references the object.. Without seeing the data from the server that'd be my first guess - jmurphyau
Thanks for your response. I wasn't very clear but the json back from the server for the status record does contain an Object in this form {id: "6", description: "Onboarding", colour: "yellow"}. In fact, in the Ember Inspector I am able to see the models so they've been pulled in ok. - danr1979
The error occurs on the line I mention above (applications.push(mymodel.serialize({includeId: true}));) within the forEach, which is serializing the model to be added in to an array. It occurs on the first model in the collection and as you can see from the stack trace I provided it is part of the belongsTo being serialized (Ember.Mixin.create.serializeBelongsTo). Does this help at all? - danr1979
Remove attrs from the iso-application-status serialiser to begin with - that's not where you specify async:true - you specify it in the model.. You given the my-model model .. What does the iso-application-status mode look like? Also, try adding this: status: DS.belongsTo('iso-application-status', { inverse: null }) - jmurphyau

1 Answers

0
votes

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.