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

时间:2017-10-24 15:28:41

标签: javascript indexeddb lokijs

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

2 个答案:

答案 0 :(得分:0)

是的,你可以使用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 apis,可以使用一些indexeddb包装器,如 - JsStore

答案 1 :(得分:0)

indexedDB.deleteDatabase 可以删除 IndexedDB 数据库,只需确保您正在侦听所有事件,以便在数据库被阻止或其他情况下收到通知。

以下是监听所有事件的承诺 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);
        });
    })
}

这只是一个例子。例如,您可能希望在 reject 事件中表现不同(即不一定是 upgradeneeded)。

示例用法:

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