Reputation: 87
I am using IndexedDB with latest chrome/chromium versions, and I still have the following problem. I try to delete the DB before creating it, as you can see in the code below.
First time I execute this code, 'onupgradeneeded' is correctly logged. At this time, the DB exists. Then if your run the same code, the database is correctly removed but the onupgradeneeded callback is not called again.
I don't find any information about that in the specs neither anywhere else. So if someone has an idea about how to solve this problem, I'm interested.
UPDATE:
<script type="text/javascript">
var module={
dbName: 'myDB',
dbVersion: 1,
dbStores:[{name:'places', keyPath:'id'}],
/* START :: DEFINE MAINONLINE INTERFACE FUNCTIONS */
test: function(){
var that= this;
this.deleteDB()
.then(function(e){
return that.createEmptyDB(e);}
)
.catch(function(error){
that.logError(error);
});
},
logError: function(e){
console.log('An error occured');
if (e.target && e.target.errorCode){
console.log('errorCode = ' + e.target.errorCode);
}else{
console.log(e);
}
},
deleteDB: function(){
return new Promise(function(resolve, reject){
var deleteDbRequest= indexedDB.deleteDatabase(this.dbName);
deleteDbRequest.onsuccess= function(e){
console.log('deleted successfully');
resolve(e);
}
deleteDbRequest.onerror= function(e){
console.log('error while deleting');
reject(e);
}
deleteDbRequest.onblocked= function(e){
console.log('blocked, unable to delete');
reject(e);
}
});
},
createEmptyDB: function(event){
var that= this;
return new Promise(function(resolve, reject){
var openDbRequest= indexedDB.open(that.dbName, that.dbVersion);
openDbRequest.onerror= function(e){
console.log('error');
reject(e);
}
openDbRequest.onblocked=function(e){
console.log('The open request is blocked');
}
openDbRequest.onsuccess= function(e){
console.log('onsuccess');
resolve(e);
}
openDbRequest.onupgradeneeded= function(e){
console.log('onupgradeneeded');
}
});
}
}
module.test();
</script>
Thanks in advance
Upvotes: 1
Views: 975
Reputation: 14499
The delete won't work the second time because the database is open and therefor the "blocked" handler is invoked.
If you do it like this it works. Notice the onsuccess function which closes the db.
var delReq = indexedDB.deleteDatabase('myDB');
delReq.onsuccess= function(e){
var openReq = indexedDB.open('myDB', 1);
openReq.onupgradeneeded= function(e){
console.log('Im called');
}
openReq.onsuccess = function() {
openReq.result.close();
}
}
delReq.onblocked = function() { console.log("blocked",arguments); }
edit
Also added some code for the onblocked event. If you comment out the openReq.result.close()
code, you will see the blocked
part
edit2
Your deleteDb function doesn't work, notice the this.dbName
. this
is window in your context. If you change it too:
deleteDB: function(){
var that = this;
return new Promise(function(resolve, reject){
var deleteDbRequest= indexedDB.deleteDatabase(that.dbName);
....
When you execute this code.. you will again see the onblocked event being fired
edit3
Part of the spec where it makes deleting a undefined
database succesfull: http://www.w3.org/TR/IndexedDB/Overview.html#dfn-steps-for-deleting-a-database --> If no database was found, then these steps are considered successful. Abort these steps
Upvotes: 3
Reputation: 87
Ok, It was not related to indexedDB. In my case, this.dbName is undefined. I should just use that instead. Sorry for the error.
However, I don't undertand why deleteDabase(undefined) doesn't throw an error...
Upvotes: 0