IndexedDB 无论我更改版本号多少次,都无法获得需要调用的升级

dpiehjr4  于 2022-12-09  发布在  IndexedDB
关注(0)|答案(1)|浏览(336)

我正在尝试创建一个IndexedDb,但由于某种原因无法调用upgradened,这意味着我无法创建对象存储。任何见解都将非常有用,以下是我正在使用的代码:

const request = window.indexedDB.open('OriginInvestmentsFrontEnd', 1);
    request.onerror = (event) => {
      console.log('error: ', event);
    };
    request.onsuccess = (event) => {
      console.log('success: ', event);
    };

    request.onupgradeneeded = (event) => {
      console.log('onupgradeneeded: ', event);
      const db = event.target.result;
      db.createObjectStore('front-end-state');
    };

我试过使用版本1、2、3、4、10和11(之前没有创建过这些版本)。无论我怎么尝试,都不会调用onupgardeneeded方法,我只会得到onsuccess方法。我在这里做错了什么?根据我的理解,版本更改应该触发onupgradeneeded,但它没有?

yzxexxkh

yzxexxkh1#

我有同样的问题,我正在改变版本,以调用onupgradeneded,但它没有得到调用
但经过研究我找到了一个解决方案

在我的情况下,连接没有接近,这就是为什么它没有触发onupgradeneeded,而是触发request.onblocked
因此也关闭request.onblocked中的连接

const request = window.indexedDB.open('OriginInvestmentsFrontEnd', 1);
db.onblocked = (event) => {
  console.log('blocked ...', event); // in my case this was triggering 
  (event.target as any).result.close(); // so close the connection and it worked
};
request.onerror = (event) => {
  console.log('error: ', event);
  (event.target as any).result.close(); // Close the connection
};
request.onsuccess = (event) => {
  console.log('success: ', event);
  (event.target as any).result.close(); // Close the connection
};

request.onupgradeneeded = (event) => {
  console.log('onupgradeneeded: ', event);
  const db = event.target.result;
  db.createObjectStore('front-end-state');
};

相关问题