问题
I want to create multiple datastore, So I found solution that I can do this on version change.
So I wrote following
var request = indexedDB.open(dbName);
request.onsuccess = function (e){
var db = e.target.result;
var version = db.version;
db.close();
var request2 = indexedDB.open(dbName , ++version);
console.log(request2); //Error on this line
request2.onsuccess = function() { console.log("success .. "); };
request2.onerror = function() { console.log("error..."); };
request2.onblocked = function() { console.log("blocked..."); };
request2.onupgradeneeded = function(e2) {
//Will creaate new datastore here
};
}
when I open database with higher version it is giving following error "error: [Exception: DOMException]"
DBOpenDBRequest {onupgradeneeded: null, onblocked: null, onerror: null, onsuccess: null, readyState: "pending"…}
error: [Exception: DOMException]
onblocked: function () { console.log("blocked..."); }
onerror: function () { console.log("error..."); }
onsuccess: function () { console.log("success .. "); }
onupgradeneeded: function (e2) {
readyState: "pending"
result: [Exception: DOMException]
source: null
transaction: null
__proto__: IDBOpenDBRequest
Thanks in advance.
回答1:
I think the version property is probably the issue. Are you sure it has a value. A DOM Exception occures when opening the db when the value of version is 0 or a negative number. For more info see the specs
When you are trying to open the db again with a higher version, you get a blocked event.
Try adding the following to the db object.
db.onversionchange = function (event) { event.target.close(); }
This will close the current connection. So the upgrade of the db can proceed. When upgrading the db you can't have any open connections.
回答2:
Your IDB is fine. It looks like you have a scoping issue inside of your second success callback. I found request2 object to be undefined but the request itself is fine.
Here's a working fiddle stripped down to the core issue (the second IDBOpenRequest) - when executing this you'll see the version increment on each run.
var dbName = 'User780611',
request = indexedDB.open(dbName);
request.onsuccess = function (e) {
var db = e.target.result;
var version = db.version;
db.close();
var request2 = indexedDB.open(dbName, ++
version);
request2.onsuccess = function () {
window.document.getElementById('User780611').innerHTML = this.result.version;
};
}
回答3:
My guess is that the printout of request 2 gives an error because it is still busy either opening the db or calling the upgradeneeded. I think - if you remove console.log(request2); - Put a try catch around the code - Put console.log into all callbacks you will see what is happening.
var request = indexedDB.open(dbName);
request.onsuccess = function (e){
var db = e.target.result;
var version = db.version;
db.onversionchange = function () { console.log("version change ...."); };
db.close();
try
{
var request2 = indexedDB.open(dbName , ++version);
//console.log(request2); //Error on this line
request2.onsuccess = function() { console.log("success .. "); };
request2.onerror = function() { console.log("error..."); };
request2.onblocked = function() { console.log("blocked..."); };
request2.onupgradeneeded = function(e2) {
//Will creaate new datastore here
console.log("upgradeneeded...");
};
}
catch(ex)
{
console.log(ex);
}
}
回答4:
I just came across the same issue. As Kristof said, 'Blocked' message means there are open connections. So the reason its still appearing despite the db.close() in the first request.onsuccess callback means that there is another database still open. Therefore, you need to close that as well before you execute the second request.
Here is how i solved it in order to add object store too:
var idb = (function(){
var idbObj = {};
var db = null; //gonna need to close this db later when adding new object store
const dbName = "collectionDB3";
var version = 1;
idbObj.open = function(){
var objectStore;
var dbRequest = window.indexedDB.open(dbName);
dbRequest.onupgradeneeded = function(event){
var thisDB = event.target.result;
if(!thisDB.objectStoreNames.contains("collection")) {
objectStore = thisDB.createObjectStore("collection", { keyPath: 'id', autoIncrement: true });
}
console.log("upgrading", thisDB);
};
dbRequest.onsuccess = function(event){
db = event.target.result;
version = db.version;
console.log("opened database", version);
};
dbRequest.onerror = function(event){
console.log("database error ", event.target.errorCode);
};
};
idbObj.addObjectStore = function(storeName){
db.close(); //this is where the only opened db connection is closed
var request = indexedDB.open(dbName, version+1); //increment version to create object store in onupgradeneeded
request.onupgradeneeded = function (event) {
var thisDB = event.target.result;
if(!thisDB.objectStoreNames.contains(storeName)) {
var objectStore = thisDB.createObjectStore(storeName, { keyPath: 'id', autoIncrement: true });
}
};
request.onsuccess = function (event) {
db = event.target.result; //update db
version = db.version; //update db's version
console.log("added objectstore", db);
};
request.onerror = function(event){
console.log("database error ", request.error);
};
};
return idbObj;
}());
idb.open();
createObjectStore(); //test the addObjectStore method see if new object store is created
function createObjectStore(){
setTimeout(function(){idb.addObjectStore("newStoreName")}, 3000);
}
Hope this helps =)
来源:https://stackoverflow.com/questions/22604556/domexception-at-the-time-of-opendatabase-with-higher-version