Search code examples
ember.jsember-cli-mirage

Ember cli mirage error: patch handler cannot read property update of null


I am using ember cli mirage with my amber app, i have data defined in fixtures and using the RestSerializer, i am trying to simulate updating the attributes of a record but getting an error: The patch handler for the url api/survey-groups/[id] threw an error: cannot read property update of null

mirage/config.js

 this.patch('/survey-groups/:id', function({ surveyGroups }, request) {
let id = request.params.id;
let attrs = this.normalizedRequestAttrs();

return surveyGroups.find(id).update(attrs);

});

mirage/serializers/application.js

import { RestSerializer } from 'ember-cli-mirage';
export default RestSerializer.extend({
primaryKey: 'keyId'});

app/serializers/application.js

import DS from 'ember-data';

export default DS.RESTSerializer.extend({
primaryKey: 'keyId', });

sample of fixture; mirage/fixtures/survey-groups.js

export default [
  {
    "code": "dfdj", 
    "description": "", 
    "keyId": 29116, 
  }, 
  {...... }]

I also noticed in the data returned by the server that an id attribute was added to each record, with a string value e.g. id: "1" When i attempt to find a record using this string value in place of the id, the record is returned.

What could be causing this error and behavior


Solution

  • ember-cli-mirage's Serializer does not have an primaryKey option. As far as I'm aware of mirage does not provide any possibility to customize the name of the primary key. Therefore you can't use find method. I would recommend to use findBy instead: return surveyGroups.findBy({ keyId: id }).update(attrs);

    Another option would be to use change the name of primary key on serialization (serialize) and normalization (normalize) of the payload. This approach has the benefit that you are still able to use mirage's shorthands.