更新 我发现问题是它被阻止了。尽管数据库始终由同一扩展程序创建和升级,但它不会被关闭。所以现在我正在调用“onblocked”函数。
如何“取消阻止”当前被阻止的数据库?我将来如何防止这种情况发生?这是一个应用程序,因此没有选项卡正在使用它。由于我无法打开这些数据库甚至删除它们(这也被阻止), 如何关闭它们?
(对于任何想知道的人,为了从一开始就避免这个问题,你有来做以下事情:)
mydb.onversionchange = function(event) {
mydb.close();
};
原帖
如果我(意外地)尝试使用错误的版本打开并升级,则IndexedDB会死并变得无法打开。据我所知,没有办法向indexedDB索取最新版本的数据库。因此,如果我尝试两次运行以下代码,它会破坏数据库,并且无法打开它:
它永远不会抛出错误或调用错误。它只是静静地坐着
var db = null;
//Note, no version passed in, so the second time I do this, it seems to cause an error
var req = indexedDB.open( "test" );
req.onsuccess = function(event) { console.log( "suc: " + event.target.result.version ); db = event.target.result; };
req.onerror = function(event) { console.log( "err: " + event ); };
req.onupgradeneeded = function(event) { console.log( "upg: " + event.target.result.version ); };
//We're doing in interval since waiting for callback
var intv = setInterval(
function()
{
if ( db === null ) return;
clearInterval( intv );
var req2 = indexedDB.open( "test", db.version + 1 );
req2.onsuccess = function(event) { console.log( "suc: " + event.target.result.version ); };
req2.onerror = function(event) { console.log( "err: " + event ); };
req2.onupgradeneeded = function(event) { console.log( "upg: " + event.target.result.version ); };
},
50
);
所有代码都在我的chrome.runtime.onInstalled.addListener
中。因此,当我更新我的应用时,它会再次调用它。如果我在没有传入新版本的情况下调用indexedDB.open( "test" )
然后再次运行setInterval函数,则会导致一切无法使用,我再也无法打开“test”了。如果我可以在尝试打开数据库之前查询indexedDB的数据库版本,这将得到解决。那存在吗?
答案 0 :(得分:1)
也许这有帮助?
function getVersion(callback) {
var r = indexedDB.open('asdf');
r.onblocked = r.onerror = console.error;
r.onsuccess = function(event) {
event.target.result.close();
callback(event.target.result.version);
};
}
getVersion(function(version) {
console.log('The version is: %s', version);
});
好的,基于convo,这个小的util函数可能会让你走上路径:
var DATABASE_NAME_CONSTANT = 'whatever';
// Basic indexedDB connection helper
// @param callback the action to perform with the open connection
// @param version the version of the database to open or upgrade to
// @param upgradeNeeded the callback if the db should be upgraded
function connect(callback, version, upgradeNeeded) {
var r = indexedDB.open(DATABASE_NAME_CONSTANT, version);
if(upgradeNeeded) r.onupgradeneeded = updateNeeded;
r.onblocked = r.onerror = console.error;
r.onsuccess = function(event) {
console.log('Connected to %s version %s',
DATABASE_NAME_CONSTANT, version);
callback(event.target.result);
};
}
// Now let us say you needed to connect
// and need to have the version be upgraded
// and need to send in custom upgrades based on some ajax call
function fetch() {
var xhr = new XMLHttpRequest();
// ... setup the request and what not
xhr.onload = function(event) {
// if response is 200 etc
// store the json in some variable
var responseJSON = ...;
console.log('Fetched the json file successfully');
// Let's suppose you send in version and updgradeNeeded
// as properties of your fetched JSON object
var targetVersion = responseJSON.idb.targetVersion;
var upgradeNeeded = responseJSON.idb.upgradeNeeded;
// Now connect and do whatever
connect(function(db) {
// Do stuff with the locally scoped db variable
// For example, grab a prop from the fetched object
db.objectStore('asdf').put(responseJSON.recordToInsert);
// If you feel the need, but should not, close the db
db.close();
console.log('Finished doing idb stuff');
}, targetVersion, upgradeNeeded);
}
}
答案 1 :(得分:0)
我认为最好始终提供版本号。如果不这样做,您将如何管理数据库结构的升级?如果你不是很好的机会,你会遇到客户端上相同的db版本将拥有其他数据库结构的情况,我认为这不是你想要的。所以我建议将版本号保存在变量中。
使用indexeddb时,您还必须提供从之前版本到当前版本的升级计划。含义版本4具有一定的结构,但您必须能够从头开始获得与版本1,2和3相同的结构