Why is my indexedDB.open.onsuccess event not being called?
I am a preface to this question so that I am new to indexed DB and asynchronous javascript. I am writing a function for a chrome app that takes an Array buffer called sourceBlob and stores it in indexedDB. The function then opens the database in indexedDB that I previously created when starting applications and starts a transaction with that database. For some reason, when I execute this code, request.onsuccess is completely skipped and therefore I cannot add my data to the objectStore. How do I enforce request.onsuccess? I have read that this problem can be caused by indexedDB being asynchronous, but I have not been able to find an efficient solution to this problem.
data.indexedDB.addSource = function(sourceBlob) {
//adds source to data base
var version = 1;
var request = indexedDB.open("content", version);
request.onerror = errorHandler;
request.onsuccess = function(e) {
db = e.target.result;
var trans = db.transaction(["nContent"], "readwrite");
var store = trans.objectStore("nContent");
var request = store.put(sourceBlob);
trans.oncomplete = function(e) {
data.indexedDB.getIndexedSource();
};
};
};
source to share
Are you sure what is being called data.indexedDB.addSource
? Since you can open your browser's JS console (even on this page) and run this:
var request = indexedDB.open("content", 1);
request.onsuccess = function(e) { console.log("onsuccess"); }
You will see "onsuccess".
I suggest that you try and make a minimal example that shows your problem. In the process, you will probably find a solution.
source to share
After working on the app for a while, I realized that I had exited the addSource function before I turned on the onsuccess event. To fix this, I added a delay at the end of the function and events are now firing as expected. Thank you for your help. Fixed code:
data.indexedDB.addSource = function(sourceBlob) {
//adds source to data base
var version = 1;
var request = indexedDB.open("content", version);
request.onerror = errorHandler;
request.onsuccess = function(e) {
db = e.target.result;
var trans = db.transaction(["nContent"], "readwrite");
var store = trans.objectStore("nContent");
var request = store.put(sourceBlob);
trans.oncomplete = function(e) {
extron.indexedDB.getIndexedSource();
};
};
var token = 100000;
while(token != 0)
{
token = token-1;
}
};
source to share