Use Ember Data with API that Doesn't Return Id Property


I’d like to consume the Star Wars API with Ember Data. Specifically, I want this call to work:

However no id property is returned from the response payload. So while the network request is successful (Status 200) I get this error in the console:

Error while processing route: apple Cannot read property 'id' of null TypeError: Cannot read property 'id' of null

I’ve tried to use:

export default DS.RESTSerializer.extend({
    primaryKey: 'name'

with no success; I get the same error.

Thanks for your assistance.

Hey @MichaelR, we have a couple endpoints in our API that don’t return ID’s so what we do is, in the serializer, take the record and grab two properties from it, combine them with an underscore, and set that as the ‘id’ property, and then pass it on.

Essentially you’ll want to do something similar in your serializer (off the top of my head I think in the normalize method). It looks like the id for the example you posted is encoded in the ‘url’ property if nothing else, so you could parse that out, but really Ember Data just needs a unique string value as the ‘id’ prop on the payload in order to correctly serialize it.

That looks like it should work. Where is the serializer? (Same identifying Folder and file names as the model?)

If you haven’t already created serializers Ember will use the defaults so I would create a new one with ember g serializer <modelname> but it looks like you had a serializer above, so if you have an existing one put it in there. Serializer should be in <project-root>/serializers, either in “application.js” if you want it to apply to all of your models or in “.js” for just one model.