Error processing route: ... Assertion failed: you tried to push data with type ... but no model could be found with this name

I am having a problem with relationships / inclusions and serializers using Ember with JSONAPI.

I have a route to show patient information:

import Ember from 'ember';

export default Ember.Route.extend({
  model(params){
    return this.store.find('patient', params.patient_id);
  }
});

      

And I have two models:

patient

:

import DS from 'ember-data';

export default DS.Model.extend(validations, {
  name : DS.attr('string'),
  email : DS.attr('string'),
  gender : DS.attr('string'),
  birthDate : DS.attr('date'),
  description : DS.attr('string'),
  anamnesis : DS.belongsTo('anamnesis')
});

      

and anamnesis

:

import DS from 'ember-data';

export default DS.Model.extend({
  smokes : DS.attr('boolean'),
  drinks : DS.attr('boolean'),
  drugUser : DS.attr('boolean')
});

      

as you can see, there is a one-to-one relationship between them.

Now I have a JSONAPI response from my Ruby on Rails backend

{  
  "data":{  
    "id":"3",
    "type":"patients",
    "attributes":{  
        "name":"paciente 3",
        "gender":"female",
        "email":"teste3@teste.com",
        "birth-date":"2017-06-04T02:59:37.435Z"
    },
    "relationships":{  
      "anamnesis":{  
        "data":{  
          "id":"2",
          "type":"anamneses"
        }
      }
    }
  },
  "included":[  
    {  
      "id":"2",
      "type":"anamneses",
      "attributes":{  
        "smokes":null,
        "drinks":null,
        "drug-user":null
      }
    }
  ]
}

      

When trying to serialize relationship

and included

it looks anamnese

(from the backend response type model anamneses

) instead anamnesis

.

Error while processing route: ... Cannot read property 'type' of null TypeError: Cannot read property 'type' of null

So I had to create PatientSerializer

to fix this issue:

import DS from 'ember-data';
import Ember from 'ember';

export default DS.JSONAPISerializer.extend({
  modelNameFromPayloadKey(payloadKey){
    if(payloadKey === 'anamneses'){
      return 'anamnesis';
    } else {
      return this._super(payloadKey);
    }
  },
  modelNameFromPayloadType(payloadType){
    if(payloadKey === 'anamneses'){
      return 'anamnesis';
    } else {
      return this._super(payloadType);
    }
  },
});

      

I know this might not be the best solution, but I can't even try something else because it shows me this error:

Error while processing route: ... Assertion Failed: You tried to push data with a type 'anamnese' but no model could be found with that name.

What am I missing? Am I fixing this wrong?

+3


source to share


1 answer


  • Change anamnesis : DS.belongsTo('anamnesis')

    to anamnesis : DS.belongsTo('anamnese')

    .
  • Change the name of the model file anamnesis

    to anamnese

    .
  • You don't need to inject a serializer Patient

    to change the model name.

The above change will work. check out this twiddle



PS: find

deprecated in the latest version, considerfindAll

+1


source







All Articles