John
John

Reputation: 9466

Ember Data does not recognise ID in JSON response (inheritance-related)

I've been trying to figure this out for the past couple of hours and I thought it might be best to ask for some help at this point.

I am creating a new record (e.g., a new ProducerObjectiveOutcome record). The following error is being thrown after my Ember app posts data from a form to my Rails API:

Assertion Failed: Your outcome record was saved to the server, but the response does not have an id and no id has been set client side. Records must have ids. Please update the server response to provide an id in the response or generate the id on the client side either before saving the record or while normalizing the response.

What's strange is that my API is returning a JSON response with an ID (see below).

{
  "producer_objective_outcome": {
    "id":27,
    "type":"ProducerObjectiveOutcome",
    "title":"New outcome",
    "owner": {
      "id":6
    }
  }
}

As you might already have picked up, ProducerObjectiveOutcome is a subclass of Outcome.

In both my Ember App and Rails API, I have setup ProducerObjectiveOutcome to inherit from Outcome. In my Ember App particular, this is what the model looks like:

// app/models/producer-objective-outcome.js

import Outcome from "./outcome";

export default Outcome.extend({

});

Nothing fancy going on here – I thought it was all pretty straight forward – but for some reason, that error is coming up. I'm hoping one of you marvelous people can help me out with this!

Thanks in advance!

Upvotes: 0

Views: 385

Answers (1)

John
John

Reputation: 9466

For anyone who is interested, I solved this by changing the name of the root node of the JSON response to outcome instead of it being producer_objective_outcome.

What may have helped in my earlier explanation is that the ProducerObjectiveOutcome record was being created at the following route: producer/objective/:id/outcome/new.

The key part of this URI, as I have figured just out, is the .../outcome/... section. I haven't tested otherwise, but given the conventions in Ember, I suspect that if that URI was .../producer-objective-outcome/... then everything would have been hunky dory.

TL;DR: the names in your JSON payload should match the route.

Upvotes: 1

Related Questions