如果数据库名称为LokiCatalog,是否有办法删除indexedDB

mzsu5hc0  于 2022-12-09  发布在  IndexedDB
关注(0)|答案(2)|浏览(142)

如果数据库名称是LokiCatalog,有没有办法从cordova应用程序的chrome控制台中删除indexedDB?

k3bvogb1

k3bvogb11#

是的,您可以使用indexeddb API“deleteDatabase”删除它。因此,删除数据库的代码“LokiCatalog将是”-

var DBDeleteRequest = window.indexedDB.deleteDatabase("LokiCatalog");

DBDeleteRequest.onerror = function(event) {
  console.log("Error deleting database.");
};

DBDeleteRequest.onsuccess = function(event) {
  console.log("Database deleted successfully");

  console.log(event.result); // should be undefined
};

更多信息,请点击此处-https://developer.mozilla.org/en-US/docs/Web/API/IDBFactory/deleteDatabase
如果你不想处理indexeddb API,你可以使用一些indexeddb Package 器,比如-JsStore

omhiaaxx

omhiaaxx2#

indexedDB.deleteDatabase可以删除IndexedDB数据库,只要确保您正在监听所有事件,这样您就可以在DB被阻塞或其他情况下得到通知。
下面是一个监听所有事件的承诺deleteDatabase版本的示例。

function deleteDb(dbName) {
    return new Promise((resolve, reject) => {
        const deleteDbRequest = indexedDB.deleteDatabase(dbName);
        deleteDbRequest.addEventListener('error', (e) => {
            console.log('delete error', e);
            reject(e);
        });
        deleteDbRequest.addEventListener('blocked', (e) => {
            console.log('delete blocked', e);
            reject(e);
        });
        deleteDbRequest.addEventListener('upgradeneeded', (e) => {
            console.log('delete upgradeneeded', e);
            reject(e);
        });
        deleteDbRequest.addEventListener('success', (e) => {
            console.log('delete success', e);
            resolve(e);
        });
    })
}

这只是一个例子。例如,你可能希望在upgradeneeded事件上有不同的行为(即不一定是reject)。
示例用法:

deleteDb('LokiCatalog')
    .then(r => console.log('success', r))
    .catch(e => console.error('oops', e));

相关问题