Ember 数据预期会在适配器响应中找到具有以下 ID 的记录,但它们丢失了

Ember Data expected to find records with the following ids in the adapter response but they were missing

当我尝试使用 ember 存储数据时,我似乎遇到了一个反复出现的问题。

当我清除我的 localforage(Chrome 开发工具 -> 选项卡应用程序)并重新加载我的应用程序时,我向我的 API 创建了一系列请求,并得到了那些 API 调用我创建并将它们存储在本地存储中。

经常,在我清除我的存储后,我会收到以下警告:

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

之后,我得到一个错误

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

这应该永远不会发生,因为我已经清理了存储空间并进行了刷新。他找到了存在但实际上不存在的数据。

有没有办法解决这个问题并真正保存数据?我试着抓住它并再次存储它。尝试 'update' 它并再次存储它,但没有用。

通常,我会收到 API 有效的 JSON 响应。 Ember 然后使用如下模型将其保存到商店。该模型与商店类型同名,在本例中为 "application-settings"

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

然后为了保存它,我使用此代码,其中 'storeKey.type' = "application-settings" 和数据 = 新数据。

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

我好像找到了解决办法。在选项卡应用程序中清除 'keyvaluepairs' 时,您只在那里清除它,但它仍在 ember-data 中。刷新的时候是'there'但是localforage里没有。

因此,我 运行 遍历我商店中的每个密钥并将其卸载。之后我清除了我的 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');
});

如果您随后调用您的函数以保存到商店,您将不会收到上述警告和错误。