Search code examples
serializationember.jsember-data

Ember: Must include an 'id' in an object passed to 'push'


Currently experiencing the following error: You must include an 'id' for failed-shotlist in an object passed to 'push'. This is in a code base I have inherited mid-development and I am fairly new to Ember.

From what I understand, this occurs when the backend does not respond with an ID. The server payload looks like the following (returning an alert object with an embedded failedShotlist record):

alertAuthor: "Test name"
alertDate:"2018-06-28T16:25:21+12:00"
alertIdentifier:"456e15c7-7a8b-11e8-84a8-06f4aef780e3"
alertType:"failedShotlist"
email:"test@gmail.com"
failedShotlist:
    projectIdentifier:"79050dfb-5faf-11e8-84a8-06f4aef780e3"
    projectName:"8888 st"
    projectRoleENUM:"bp"
    projectRoleName:"Building Participant"
    shotlistDescription:"Framing"
    shotlistIdentifier:"79d52773-5faf-11e8-84a8-06f4aef780e3"
inviteIdentifier:null
profileId:"c4e02bee-3d26-11e8-84a8-06f4aef780e3"
shotlistIdentifier:"79d52773-5faf-11e8-84a8-06f4aef780e3"

Since the backend doesn't respond with an ID attr, the primary key needs to be transformed using a serializer's 'primaryKey' property:

serializers/alert.js

export default ApplicationSerializer.extend(EmbeddedRecordsMixin, {
  primaryKey: 'alertIdentifier',

  attrs: {
    'invite': { deserialize: 'records' },
    'failedShotlist': { deserialize: 'records' },
  },
});

I couldn't find any mention of this, but I assume that embedded records are further serialized by their own serializers. The existing one is as follows:

serializers/failedShotlist.js

export default ApplicationSerializer.extend({
  attrs: {
    'shotlistId': { key: 'shotlistIdentifier' },
    'projectId': { key: 'projectIdentifier' },
  },
});

Since the ID's for the failedShotlist object also need to be transformed, I have updated this to include the primaryKey prop:

serializers/failedShotlist.js

export default ApplicationSerializer.extend({
  primaryKey: 'shotlistIdentifier',

  attrs: {
    'shotlistId': { key: 'shotlistIdentifier' },
    'projectId': { key: 'projectIdentifier' },
  },
});

Unfortunately, this results in the same error I originally encountered. Any ideas as to how this might be resolved?


Solution

  • Something I had overlooked was that the source files for the adapter and the serializer weren't following the naming convention of the rest of the codebase.

    Where the serializer was called failedShotlist.js, the model related to it was called failed-shotlist.js.

    Renaming the serializer file to failed-shotlist.js allowed my existing code to work:

    export default ApplicationSerializer.extend({
      primaryKey: 'shotlistIdentifier'
    }