我在我的Vue + Laravel应用程序中添加了一个indexedDB,这是我目前正在使用的代码:
export default {
mixins: [VueGoogleMap.MapElementMixin],
created() {
this.eventBus.$on("refresh-map", () => {
this.fetchHexagons();
});
const getAllBuildings = () => Vue.http.get("/sandbox/buildings");
getAllBuildings().then((result) => {
const async = Dexie.async;
const spawn = Dexie.spawn;
const db = new Dexie("buildings");
db.version(1).stores({
buildings: `++dexie_id, id, name, address, city, nr_units, lat, long, purchased`,
});
db.transaction("rw", db.buildings, function* () {
result.body.forEach(function*(el) {
const a = yield db.buildings.add({
id: el.id,
name: el.name,
address: el.address,
city: el.city,
nr_units: el.nr_units,
lat: el.lat,
long: el.long,
purchased: el.purchased,
});
});
console.log("done");
}).catch(function(err) {
// Catch any error event or exception and log it:
console.error(err.stack || err);
});
正如您所看到的,我正在查询数据库,以便传输indexedDB中的所有建筑物,以便更快地解析它们(我需要在地图中显示它们)。问题是,当我运行此代码时,我收到以下错误:
CanvasOverlay.vue?e208:68 Error: Failed to execute 'transaction' on 'IDBDatabase': One of the specified object stores was not found.
at Transaction.create (dexie.es.js?f8d5:2900)
at eval (dexie.es.js?f8d5:2222)
at eval (dexie.es.js?f8d5:1343)
at callListener (dexie.es.js?f8d5:1026)
at endMicroTickScope (dexie.es.js?f8d5:1113)
at IDBOpenDBRequest.eval (dexie.es.js?f8d5:1180)
我安装的唯一包是Dexie:
npm install --save dexie
我是否需要安装更多内容或者可能是Webpack问题?
答案 0 :(得分:0)
尝试删除数据库并重新运行代码。此问题非常常见,并且在根据these docs更新相同版本(1)而不增加版本号时会发生此问题。
另外,我看到另一个错误 - 你不能在forEach()调用中使用function *。而是使用:
for (let el of result.body) {
...
}
甚至更好:
const itemsToAdd = result.body.map(el => ({
id: el.id,
name: el.name,
address: el.address,
city: el.city,
nr_units: el.nr_units,
lat: el.lat,
long: el.long,
purchased: el.purchased,
}));
return db.buildings.bulkAdd(itemsToAdd);