Assertion Failed: You need to pass a model name to the store's modelFactoryFor

Hey there!

I have these models that has belongsTo relationships:

//app/models/primary.js
export default DS.Model.extend(Validations, {
  number: DS.attr('string'),
  protocolDate: DS.attr('date'),
  document: DS.belongsTo('document'),
  origin: DS.belongsTo('origin')
});
//app/models/document.js
export default DS.Model.extend(Validations, {
  number: DS.attr('string'),
  name: DS.attr('string')
});
//app/models/origin.js
export default DS.Model.extend(Validations, {
  number: DS.attr('string')
});

And these serializers:

//app/serializers/application.js
import Ember from 'ember';
import DS from 'ember-data';

export default DS.RESTSerializer.extend({
  normalizeSingleResponse(store, primaryModelClass, payload, id, requestType) {
    var typeKey = primaryModelClass.modelName;
    return this._normalizeResponse(store, primaryModelClass, payload, id, requestType, true);
  },
  
  normalizeArrayResponse(store, primaryModelClass, payload, id, requestType) {
    var pluralTypeKey = Ember.Inflector.inflector.pluralize(primaryModelClass.modelName);
    return this._normalizeResponse(store, primaryModelClass, payload, id, requestType, false);
  },

  serializeIntoHash: function(hash, type, record, options) {
    Ember.merge(hash, this.serialize(record, options));
  },

  keyForAttribute(attr) {
    return Ember.String.camelize(attr);
  }

});
//app/serializers/primary.js
import DS from 'ember-data';
import ApplicationSerializer from './application';

export default ApplicationSerializer.extend(DS.EmbeddedRecordsMixin, {
  attrs: {
    document: {
      embedded: 'always'
    },
    origin: {
      embedded: 'always'
    },
  }

});

//app/serializers/document.js
import DS from 'ember-data';
import ApplicationSerializer from './application';

export default ApplicationSerializer.extend(DS.EmbeddedRecordsMixin, {
});

//app/serializers/origin.js
import DS from 'ember-data';
import ApplicationSerializer from './application';

export default ApplicationSerializer.extend(DS.EmbeddedRecordsMixin, {
});

But I’m still getting the error: Assertion Failed: You need to pass a model name to the store's modelFactoryFor method Error

I’m not sure why I’m getting this error. Here’s my payload:

{
    "primary": {
        "id": "2",
        "protocol_date": "2017-09-25 06:00:00",
        "number": "123452017",
        "document": {
            "id": "2",
            "number": "12321321",
            "name": "Tipo 2 Documento Originário"
        },
        "origin": {
            "id": "3",
            "number": "45682017"
        }
    }
}

My guess is that I have to change my payload to fit the JSON payload conventions, but I don’t want to change my API. Do you have any idea how could I fix this? Thank you all!

Want to set up a twiddle?

Thank you! I did manage to solve this issue. I had to work on the modelNameFromPayloadKey method to make it return the correct model name whether it is pluralized or not.

1 Like