在firefox中打开indexedDB数据库后,是否可以更改其结构? [英] Can you ever alter the structure of an indexedDB database after it has been opened in firefox?

查看:77
本文介绍了在firefox中打开indexedDB数据库后,是否可以更改其结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var db;
var version = 1;
var request = indexedDB.open("myDB", version);
request.onsuccess(function(e) {db = e.target.result;});
// db.close(); //??? happens async and has no callback
var request2 = indexedDB.open("myDB", ++version);
request.onsuccess = function() { console.log("success"); };
request.onerror = function() { console.log("error"); }; // called if db existed when page was loaded
request.onblocked = function(){console.log("blocked");}; // called on init creation
request.onupgradeneeded = function(){console.log("onupgradeneeded");};

我需要能够打开数据库,读取对象存储,然后更改数据库.看起来每个页面加载一次只能更改db结构.

I need to be able to open the db, read an object store, and then alter the db. It looks like you can only alter the db structure once per page load.

当使用不建议使用的setVersion方法时,此方法在Chrome中效果很好.

This works just fine in Chrome when using the deprecated setVersion method.

推荐答案

IndexedDB API并不易于使用.一些事情:

The IndexedDB API isn't easy to use. A few things:

1)直到没有其他打开的数据库连接时,才会触发需要升级.取消注释db.close()行.但是,在请求收到成功事件之前,db不会是IDBDatabase对象,因此您必须等待.

1) upgradeneeded won't fire until there are no other open connections to the db. Uncomment the db.close() line. But db won't be an IDBDatabase object until request has received a success event, so you have to wait for that.

2)request2对象上没有事件处理程序.您可能打算在代码示例的最后4行上放置request2而不是request.

2) The request2 object has no event handlers on it. You probably meant to put request2 instead of request on those last 4 lines in the code sample.

3)第一个request.onsuccess分配错误.

3) The first request.onsuccess assignment is wrong.

4)如果磁盘上的数据库的版本高于您要打开的版本,则将调用错误处理程序.

4) The error handler will be called if the the database on disk has a version higher than the one you are passing to open.

尝试一下:

indexedDB = indexedDB || mozIndexedDB;
var db;
var request = indexedDB.open("myDB");
request.onsuccess = function(e) {
    db = e.target.result;
    var version = db.version;
    db.close();
    var request2 = indexedDB.open("myDB", ++version);
    request2.onsuccess = function() { console.log("success"); };
    request2.onerror = function() { console.log("error"); };
    request2.onblocked = function() { console.log("blocked"); };
    request2.onupgradeneeded = function() { console.log("onupgradeneeded"); };
};

控制台将显示:

onupgradeneeded
success

如果不是:

  1. 检查是否没有其他选项卡打开了与此数据库的连接.
  2. 添加要请求的其他三个事件的处理程序,查看哪个事件触发.

这篇关于在firefox中打开indexedDB数据库后,是否可以更改其结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆