0

I am trying to destroy a record and I get this error

An adapter cannot assign a new id to a record that already has an id.
[…] had id: 25 and you tried to update it with null. This likely happened because
your server returned data in response to a find or update that had a different
id than the one you sent.

My REST API returns a 200 status code with an empty object response {}. I assume that’s where the problem lies, so I have been trying to customize several serializer hooks (normalizeDeleteRecordResponse, extractDeleteRecord, or even just normalizeResponse) but none of those get actually called.

Looking at my stack trace, the error seems to be in the didSaveRecord hook, which I assume is receiving the empty JSON payload and passing it to updateId.

2 Answers 2

5

The default adapter for Ember Data follows the JSON API specification so when deleting a record (or resource as they are called in the spec) you should return a 204 No Content response (without content) or a 200 OK if returning other meta data (which must be in a node named meta). Just returning an empty object with 200 OK is invalid in the spec and your best solution would be to fix your rest api to follow the spec.

Now if that is completely impossible, you could probably fix this by creating a custom adapter based on JSONAPIAdapter and then override deleteRecord. Possibly something like this based on the default implementation:

deleteRecord(store, type, snapshot) {
  var id = snapshot.id;

  return this.ajax(this.buildURL(type.modelName, id, snapshot, 'deleteRecord'), "DELETE")
    .then(response => {
      if(Object.keys(response).length === 0) {
        return null; // Return null instead of an empty object, this won't trigger any serializers or trying to push new data to the store
      }
      return response;
    });
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks a lot! This works, except it is Object.keys(response).length. I was going crazy. This is sorta unintuitive to me, though. If you don't mind could you clarify this: I assumed Adapters are to adapt data sent to the server to make it as the back-end expects, while Serializers are to adapt data from the server to make it like Ember expects it. That's why I was looking at Serializers. Am I missing something? Is there a place where can I find the order in which all these hooks are run?
Adapters and serializers both work with both incoming and outgoing data in Ember Data. Adapters are the communications layer which handles communicating with your api, be it via xhr, websockets or anything else. What it returns is a promise which returns the data as an object which is passed to the serializer which turns it into an DS.Model. On the way out the serializer turns the record (or a snapshot of it actually) back into a regular object that the adapter then sends to your api.
I had a quick look through the docs and repositories to find anything good about the order in which Ember Data and the best I could come up with is this: architecture-overview It does't take serializers into account though..
Tack så mycket for your explanation! It's a bit clearer now, though I would definitely enjoy a bit more clarity on the order of the execution of the hooks and such. I think the docs are a bit lacking on this. Thanks again for the resources.
0

Your API for delete should return 204 status code

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.