【发布时间】:2018-04-26 01:13:48
【问题描述】:
在我的项目中,我通过包装器 Dexie.js 使用 indexedDb。目标是直接从 indexedDb 中获取数据,而不是一直调用 api。
由于我可能会不时更改/删除一些数据,因此我使用版本号来跟踪用户缓存中存在的内容。因此,当应用程序启动时,我会收到数据库的版本,如果与缓存中的版本匹配,我将获取 indexedDb,否则我将删除 indexedDb 并从服务器获取新数据。
问题是我找不到在不添加另一个条目的情况下更新版本值的方法。现在这就是我正在做的事情:
db.version(1).stores({
buildings: "id, name, address, lat, long, purchased, [lat+long]",
versions: "id, version",
});
db.open().then((el) => {
console.log(el);
this.eventBus.$on("refresh-map", () => {
this.fetchHexagons();
});
}).catch(Dexie.MissingAPIError, (e) => {
console.log("Couldn't find indexedDB API");
console.log(e);
rollback = true;
}).catch((error) => {
console.log(`error: ${error}`);
});
db.versions.toArray((res) => {
if (res.length > 0 && res[0].version != version.body.model_version) {
db.buildings.clear().then(() => {
console.log("clearing buildings");
});
}
db.versions.update(1, { version: "2"}).then((update) => {
if (!update) {
db.versions.put({ version: version.body.model_version });
}
});
问题是我收到以下错误:
dexie.es.js?f8d5:1384 Unhandled rejection: Error: Failed to execute 'put' on 'IDBObjectStore': Evaluating the object store's key path did not yield a value.
我只想确保每当我添加/放置版本字段时,我只替换该值而不添加任何其他内容:这样“版本”表中的数据库将始终只有一个元素。
我该如何解决这个问题?
【问题讨论】: