【问题标题】:Dexie.js indexedDB can't find specified objectDexie.js indexedDB 找不到指定的对象
【发布时间】:2018-04-07 13:41:40
【问题描述】:

我正在我的 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 问题?

【问题讨论】:

  • 能否请您更改问题的标题,以便对其他人更有用?就目前而言,您的标题非常广泛。您的问题专门针对 dexie 的问题以及特定类型的 indexedDB 错误。请考虑提及这些事情。

标签: webpack indexeddb dexie


【解决方案1】:

尝试删除数据库并重新运行代码。这个问题很常见,当更新相同的版本 (1) 而不根据 these docs 增加版本号时会发生此问题。

另外,我看到另一个错误 - 您不能在 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);

【讨论】:

    猜你喜欢
    • 2016-12-24
    • 2016-08-25
    • 2021-01-20
    • 2023-03-14
    • 2016-07-31
    • 1970-01-01
    • 2020-01-09
    • 2014-04-15
    • 1970-01-01
    相关资源
    最近更新 更多