Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emberdata store.createRecord() , pushes duplicated records after save() #4421

Closed
v3ss0n opened this issue Jun 5, 2016 · 10 comments
Closed

Comments

@v3ss0n
Copy link

v3ss0n commented Jun 5, 2016

route

import Ember from 'ember';

export default Ember.Route.extend({

  model() {
    this.store.createRecord('dupe', {
      "message": "hello world one",
      "posted": "2016-06-05T10:11:01.473Z",
      "user": "v3ss0n"
    }).save()
  return this.store.findAll("dupe")
}
});

model

import Model from 'ember-data/model';
import attr from 'ember-data/attr';

export default Model.extend({
  message: attr('string'),
  posted: attr('date', {
    defaultValue() { return new Date(); }
  }),
  user: attr('string')
});

POST response after .save()

{"dupe": [{"id": "25166986-4a6b-48d8-a46a-8bdc6cd89ab1", "message": "hello world four", "posted": "2016-06-05T10:11:01.473Z", "user": "v3ss0n"}]}

GET response for .findAll()

{"dupe": [{"id": "25166986-4a6b-48d8-a46a-8bdc6cd89ab1", "message": "hello world four", "posted": "2016-06-05T10:11:01.473Z", "user": "v3ss0n"}]}

Results : duplicated records with same ID.

@code0100fun
Copy link
Contributor

I'm able to verify that there are duplicates in this case. Here is a twiddle that highlights the problem.

The issue can be mitigated by waiting for the save to complete before finding all:

import Ember from 'ember';

export default Ember.Route.extend({
  model() {
    return this.store.createRecord('dupe', {
      "message": "hello world one",
      "posted": "2016-06-05T10:11:01.473Z",
      "user": "v3ss0n"
    }).save().then(() => {
        return this.store.findAll('dupe');
    });
  }
});

Working on figuring out why this happens.

@bmac
Copy link
Member

bmac commented Jun 27, 2016

Thank you @code0100fun.

@v3ss0n
Copy link
Author

v3ss0n commented Jun 27, 2016

thanks a lot for verifying this @code0100fun !
So this is verified as a bug?

@diasjorge
Copy link

I'm experimenting the same issue. I create a new record in a route 'services.new' after I save I transition to the services.index view and I see the record twice. When looking at ember inspector I see the record with an id and without an id. Once I refresh the page then it goes away, so it seems there are two copies in memory.

@code0100fun
Copy link
Contributor

I was able to expose this in a test also. Still trying to track down exactly where the problem is. Pretty sure it will be somewhere in addon/-private/system/record-array-manager#updateFilterRecordArray.

If anyone else has experience with RecordArray and can identify this quickly, feel free to use this as a starting point or ping me and I can finish it.

@v3ss0n
Copy link
Author

v3ss0n commented Jun 27, 2016

@diasjorge same behavior. when refreshes it goes away, no more duplicates.

@diasjorge
Copy link

In my case and this is maybe a different issue. When doing a createRecord operation the server was not responding with the structure expected by the rest adapter, so I fixed it and now the duplicates are gone.

@axelhodler
Copy link

In my case and this is maybe a different issue. When doing a createRecord operation the server was not responding with the structure expected by the rest adapter

same here, my issue was that the response to the POST request did not contain the id property. Happens if you respond with the same body you got in the request, although your backend gave the new resource an id.

@v3ss0n
Copy link
Author

v3ss0n commented Sep 10, 2016

anything new on this?

@pangratz
Copy link
Member

@v3ss0n this is currently a known limitation of Ember-Data, tracked in #4262. As pointed out by @code0100fun in #4421 (comment) you can work around it by waiting for the response of createRecord, before you trigger the findAll.

Another solution is to ensure that store.findAll() is invoked and data is returned from the server before the newRecord.save() call is made. By this the auto updating array returned by store.findAll() will automatically contain the new record once it is returned from the server.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants