Missing id on first save on a new object


#1

Hi guys,

I’m facing a completely twisted issue right now.

When I create a new model and then persist it to my API, ember-data doesn’t handle server’s responses and therefore, ignore the ID that has been provided.

I made a lot of investigation around this, here’s what I found at the moment of writing :

Only happen if the creation process is the first save(). That mean that if I edit something, save it, and then I create my very same object : everything is perfect.

Also, if I create several objects (even of different model class), only the first 1 will be missing his reload.

I can assure that the JSON response is always valid. If I create an object 2 times with the same data, the second one is reloaded correctly. Even if my Chrome Devtool shows me that it actually received 2 ‘201’ responses with correctly formatted data.

This post sort of complete the try I made on StackOverflow yesterday (some code is available there) : HERE

Thanks a lot in advance… this is a critical issue and I cant get forward in my project until it’s not fixed…

MASSIVE UPDATE

I was sure the problem wasn’t on my side, so I decided to search in ember-data beta 7 itself and found my edge case.

The function extractSingle, which appears to be used to load the server’s response, returned undefined in my error case and returned the proper data in the following saves (as I said, I only get my problem on the first save. So good so far, I found in which part of Ember-Data my problem is.

But what is it ? those lines [3021 - 3025] :

// legacy support for singular resources
if (isPrimary && Ember.typeOf(payload[prop]) !== "array" ) {
     primaryRecord = this.normalize(primaryType, payload[prop], prop);
     continue;
}

In my error case, the if evaluation returns false because of

isPrimary = type.typeKey === primaryTypeName;

When I logged those 2 values I got category and Category

** So, TL;DR **

If the first model.save() that is triggered is via a createdRecord, the primaryTypeName is capitalized.

I fixed it by doing a non case-sensitive comparison

isPrimary = type.typeKey.toLowerCase() === primaryTypeName.toLowerCase();

Any thoughts about where this first capitalization may come from ? If the case don’t have no impact on this evaluation, maybe it would be a good fix for the next beta release to prevent futures headaches.


#2

I ran into something similar, and looked here: DS.Store.modelFor only assigns typekey when called with a string; and it assigns it to the key that is passed. This means that Category.typeKey might be set to "Category" if store.modelFor is passed store.modelFor("Category") the first time it is called.

In DS.Store.modelFor:

factory.typeKey = normalizedKey.split(':', 2)[1];```

#3

I still don’t understand why this is not documented anywhere, my issue on github is being completely ignored.

What I understand here, we are only a few to experiment this since we talk about a super basic feature…

What are we doing wrong here ?


#4

I ran into the same problem with ember 3.0.4 (ember-data 3.0.0). So this is after four years still an issue?


#5

I can’t really tell if (1) there’s actually an issue here or (2) it’s the same as the very old one above, because there’s not enough info to reproduce.

createRecord has plenty of tests, and lots of apps are using it all the time without hitting this. So if you can reproduce this behavior, please share how in a github issue.