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

Unable to create record once find has been called #2150

Closed
lacek opened this issue Aug 1, 2014 · 6 comments · Fixed by #2467
Closed

Unable to create record once find has been called #2150

lacek opened this issue Aug 1, 2014 · 6 comments · Fixed by #2467

Comments

@lacek
Copy link

lacek commented Aug 1, 2014

I am using FixtureAdapter for testing. I am trying to achieve the following:

  • find in the store if a model with certain id

  • if such model exist, it is used for processing

  • otherwise, a new model with that id is created for processing

    this.store.find('MyModel', { id: 'foo' }).then(function(model) {
        //do something with model
    }, function(reason) {
        var model = this.store.createRecord('MyModel', { id: 'foo' });
        //do something with model
    });
    

However, what I get is an error:
Error: Assertion Failed: The id foo has already been used with another record of type MyModel.

After tracing the Ember Data code, I found that:

There is a similar issue in emberFire. My temporary workaround is to call store.dematerializeRecord before store.createRecord and it is obviously undesirable because store.dematerializeRecord is non-public.

@sly7-7
Copy link
Contributor

sly7-7 commented Aug 1, 2014

Sounds like a bug, but maybe for now you can use store.getById('MyModel', id) wich is synchronous and return the record if in the store, and null if not.

@lacek
Copy link
Author

lacek commented Sep 23, 2014

Seems to be the same problem in #1831 and #1931

@oskarrough
Copy link

I'm believe to have the same issue with ember 1.7.0, ember-data.beta-10 and emberFire 1.2.6.

this.store.find('user', authData.uid).then(function(users) {
    console.log('found a user');
}, function(users) {
    console.log('no user found');
    // this.get('store').dematerializeRecord(users); // wrong syntax?
    this.createUser(authData);
}.bind(this));

results in

Uncaught Error: Assertion Failed: Error: Assertion Failed: The id facebook:xxx has already been used with another record of type app@model:user:.

@stefanpenner
Copy link
Member

im not sure this is a bug, if a request fails we still have a model.. You must either unload it or reuse it.

Maybe in the case of find, we can prevent this.

@oskarrough
Copy link

@stefanpenner, ok. How would you unload it? Unsure how to handle (unloadRecord()). Continuing my example above, I tried the following without any luck.

this.store.find('user', authData.uid).then(function(users) {
    console.log('found a user');
}, function(users) {
    console.log('no user found');
    this.get('store').unloadRecord(users);
    this.createUser();
}.bind(this));
TypeError: undefined is not a function
    at Ember.Object.extend.unloadRecord (http://0.0.0.0:4200/assets/vendor.js:72774:16)

@kubz
Copy link

kubz commented Jan 6, 2015

@oskarrough I got the same issue, by looking here : #1523 I discovered the method : @store.recordForId(type, properties.id)

the process is:
var a = store.recordForId('model',id);
store.unloadRecord(a);
store.createRecord....

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

Successfully merging a pull request may close this issue.

5 participants