我正在使用IndexedDB尝试存储数组。该数组正在缓存中,我可以在开发工具中看到它,但是由于某种原因,我遇到了以下错误:
这是导致错误的代码:
private async getExistingColumnsFromIndexDb() {
let doesCacheExist: boolean;
try {
doesCacheExist = await this.indexedDbService.getKeyIsValid(SELECTED_COLUMNS_KEY, IndexedDBTableEnum.EMPTY_PLANNING_TASKS);
if (doesCacheExist) {
let cachedData: ISelectedColumns[];
try {
cachedData = await (this.indexedDbService.getCacheIfValid(SELECTED_COLUMNS_KEY, IndexedDBTableEnum.EMPTY_PLANNING_TASKS) as Promise<ISelectedColumns[]>);
return cachedData;
} catch (error) {
// eventually log this to database - just conosole error for now and return empty array
console.error('error getting column cache', `error: ${error}`);
return [];
}
} else {
return [];
}
} catch (error) {
// eventually log this to database - just conosole error for now and return empty array
console.error('error validating if cache exists', `error: ${error}`);
return [];
}
}
public async updateColumns(columns: any[]) {
let existingColumns: ISelectedColumns[];
try {
existingColumns = await this.getExistingColumnsFromIndexDb();
} catch (error) {
// eventually log this to database - just conosole error for now
console.error('failed retrieving existing columns tasks', `error: ${error}`);
}
return this.indexedDbService.putOnTable(
[...columns.map(column => ({ columns: column} as ISelectedColumns)), ...existingColumns],
CacheDurationEnum.NEVER,
SELECTED_COLUMNS_KEY,
IndexedDBTableEnum.EMPTY_PLANNING_TASKS
);
}
public async getSelectedColumns() {
return this.getExistingColumnsFromIndexDb();
}
如果我在updateColumns方法中未包含SELECTED_COLUMNS_KEY
,则不会显示该错误。但是,由于我在上述方法中使用了密钥,因此我没有找回数据。