1
votes

I seem to have a recurring problem when i'm trying to store data using ember.

When i clear my localforage (Chrome Dev Tools -> tab Application) and reload my application, I create a series of request to my API and with the results of those API calls I create and store them in my localstorage.

Oftentimes, right after i cleared my storage i get the following warning:

Ember Data expected to find records with the following ids in the adapter response but they were missing: [1,2]

After that, I get an error

Assertion Failed: The id 1 has already been used with another record for modelClass ocularium-frontend@model:application-settings:

This should never happens since I've cleared my storage and did a refresh. He finds data that's there but actually not there.

Is there a way to fix this and actually save the data? I tried catching it and storing it again. Tried to 'update' it and storing it again, didn't work.

Normally, I get an API response with valid JSON. Ember then uses a model like below, to save it to the store. The model has the same name as the store-type, in this case "application-settings"

import DS from 'ember-data';
export default DS.Model.extend({ name: DS.attr() });

And then to save it I use this code where 'storeKey.type' = "application-settings" and data = the new data.

this.store.createRecord(request.storeKey.type, data).save();

1
You need to provide more information: what kind of API do you use, what your response looks like, what is exact code you use when trying to save the records.Senthe
I updated my question, the code to save it was already written downDriezzz

1 Answers

0
votes

I seem to have found a solution. When clearing your 'keyvaluepairs' in the tab Application you only clear it there but it's still in ember-data. When refreshing it's 'there' but not in the localforage.

Therefore I run over every key in my store and unload it. After that i clear my localforage.

let store = this.get('store');
for (let key in store.typeMaps) {
    store.unloadAll(store.typeMaps[key].type.modelName);
}

window.localforage.clear().then(() => {
    Ember.Logger.log('EMBER-DATA STORAGE CLEARED');
});

If you then call your functions to save to the store, you won't get above warnings and errors.