Data: Emberdata store.createRecord() , pushes duplicated records after save()

Created on 5 Jun 2016  路  10Comments  路  Source: emberjs/data

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.

Most helpful comment

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.

All 10 comments

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.

Thank you @code0100fun.

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

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.

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.

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

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.

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.

anything new on this?

@v3ss0n this is currently a known limitation of Ember-Data, tracked in #4262. As pointed out by @code0100fun in https://github.com/emberjs/data/issues/4421#issuecomment-228631272 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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ef4 picture ef4  路  4Comments

Robdel12 picture Robdel12  路  5Comments

bekicot picture bekicot  路  4Comments

NullVoxPopuli picture NullVoxPopuli  路  5Comments

stefanpenner picture stefanpenner  路  4Comments