I'm stuck with a problem related to db.transaction().objectStore.onsuccess i need to get back the data to create a table with the results.
As first test, i try to return data from the call after the cursor has been read completely:
function readAll() {
var resultData=[];
var objectStore = db.transaction("employee").objectStore("employee");
objectStore.openCursor().onsuccess = function(event, args) {
var cursor = event.target.result;
if (cursor){
resultData.push(cursor.value);
cursor.continue();
}else{
return resultData;
}
};
}
but this doesn't work because the asynchronous nature of the indexedDB
so, after some thinking i try to use a callback...
function readAll(callBackFunction) {
var resultData=[];
var objectStore = db.transaction("employee").objectStore("employee");
objectStore.openCursor().onsuccess = function(event, args) {
var cursor = event.target.result;
if (cursor){
resultData.push(cursor.value);
cursor.continue();
}else{
callBackFunction;
}
};
}
this seem partially work because the callback will be called but I'm not able to pass parameters to the callback function because parameters array is empty inside the onsuccess=function() (as i can understand parameters are bind to readAll function) and I'm not able to pass it inside the onsuccess. So how can i pass parameters to callback?
There is another option different from the callback?
thank you
UPDATE
My problem is that i need to pass more parameters other than the result, like "table_id", "header" etc. so i need to pass some parameters to readAll and pass those parameters to callback, something like: readAll(callBackFunction, "table_id", "header_array")
I've already tested below solution that doesn't work:
function readAll(callBackFunction) {
var resultData=[];
var objectStore = db.transaction("employee").objectStore("employee");
objectStore.openCursor().onsuccess = function(event, args) {
var cursor = event.target.result;
if (cursor){
resultData.push(cursor.value);
cursor.continue();
}else{
callBackFunction(parameters[1], parameters[2], resultData);
}
};
}
readAll(createTable, 'table_id', ['username', 'email']);
the problem is that the parameters array is empty inside the closure
UPDATE 2
arguments array is empty inside the closure but is ok outside. If i move the callback outside the closure, I've have parameters but not data..
UPDATE 3
Also adding all parameters to function seem not working.
function readAll(callback, header, idName, classToAdd, resultData) {
var resultData=[];
var objectStore = db.transaction("employee").objectStore("employee");
objectStore.openCursor().onsuccess = function(event) {
var cursor = event.target.result;
if (cursor){
resultData.push(cursor.value);
//alert("Name for id " + cursor.key + " is " + cursor.value.name + ", Age: " + cursor.value.age + ", Email: " + cursor.value.email);
cursor.continue();
}else{
//HERE ALL ARGUMENTS ARE EMPTY BUT resultData Exists!
callback(arguments[1], arguments[2], arguments[3], arguments[4], resultData);
}
};
//HERE ALL ARGUMENTS EXISTS EXCEPT resultData!
callback(arguments[1], arguments[2], arguments[3], arguments[4], resultData);
}
You could just invoke the function like
callBackFunction(resultData);
so that the function you passed receive the results. In the function declaration you should have such a parameter:
function callbackFunction(results) {
//create the table
}
You would pass it to readAll()
like
readAll(callbackFunction);
EDIT:
According to your edit. You have to add extra parameters to your function declaration
function readAll(callBackFunction, table_id, data) {
// ... code
callBackFunction(table_id, data, resultData);
}
Then you can declare 2 more parameters in your createTable function and they will receive "table_id" and "data".
after some time I'm back to this problem and find a solution that work. The problem is that I initialize the objectStore in a single call.
var objectStore = db.transaction("employee").objectStore("employee");
if I divide this call in two different pieces:
var transaction = db.transaction("employee");
var objectStore = transaction.objectStore("employee");
everything work as expected.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.