Error when updating Model with relationship


#1

Hi folks,

I set up a simple Ember Twiddle to show you my error that is occurring when trying to save a model. It’s considerable that I’m using ember-cli-mirage for mocking the data. According to the docs, I created a shorthand route that should handle the PUT request. It does, but with the error: Your handler for the url /api/shops/1 threw an error: Cannot convert undefined or null to object

When using the JSONAPISerializer, everything is working with shorthands (mirage/config.js) and I’m able to update models, but in my case I have to use the RESTSerializer with serialized IDs in the responses. The request payload when I’m sending the model’s attrs are without Id at the end of the property name, f.e.:

// attrs object in PUT request
{
  name: "Shop 1",
  city: "1" // belongsTo relationship,
}

Now Mirage is trying to find those properties on the respective database model that has to be updated, but cannot find it, because in the database it’s cityId and not just city

My question is: Is there a way to stick to shorthand route handlers only, or do I really have to write a helper or other custom solution, only because I have to use the RestSerializer? That would be really sad, but at least I would know then.

Thanks for your support!

ChrizzDF


#2

I think in this case you’d want to use a Mirage serializer and override the “normalize” method (used to serialize a POST/PUT payload into mirage format) to munge the data into the format that mirage expects.

EDIT: that would allow you to still use the shorthand route handlers. You can define an application level serializer in mirage, or do it on a per-model basis.


#3

Hi @dknutsen,

thanks for your reply!

Yeah, I also found this solution in this issue report and it’s working, but I was hoping I could avoid something like this, because as far as I can remember, in previous versions of ember-cli-mirage (v0.1.x) it was also not needed to override the normalize method in the serializer to be able to make use of the RestSerializer with serializedIds…

Thanks again!


#4

Yeah I believe the mirage serializer layer was added since then. It makes mirage a lot more powerful but the downside is that in situations where you’re mocking a non-standard API (aka most of them) you have to do a little more customization work. We have to use the mirage serializers for a number of things to get our mirage setup to match our API. Anyway, good luck!