Christoph Langhof
Christoph Langhof

Reputation: 46

Not possible to use shorthand route handlers if RestSerializer is used? (ember-cli-mirage)

I set up a simple Ember Twiddle to show you my error that is occurring when trying to update 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...

I also found this issue report and it’s working, but I was hoping I could avoid something like this. 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…

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!

Upvotes: 1

Views: 214

Answers (1)

acorncom
acorncom

Reputation: 5955

Short answer: it looks like you need the custom serializer for now until the bug fix for it is merged.

Long answer: that issue looks to be an issue that occurred in the 0.2 -> 0.3 upgrade for Mirage, likely because of underlying DB changes made in Mirage. It'll probably get fixed, but for now you'll need to work around it.

Upvotes: 0

Related Questions