websql成功回调查询后无法访问变量

时间:2021-11-15 19:31:15

related questions(not working):

相关问题(不工作):

scope-troubles-in-javascript-...

Pass extra parameters to WebSQL callback function?

将额外的参数传递给WebSQL回调函数?

I have a 'log' object to capture a few success or error variables as the websql transaction loops through the queries. There is a loop in a loop that is cycling through the data input which is provided from the server in the form of objects in arrays in objects, basically tables then rows. This all works fine until the internal success callback of the sql-query (not the final transaction success.) is called. as you can see from the below i've tried to call a function builder function to capture the table name variable but it is undefined when the returned function is called. I have tried many ways but i either end up with undefined or the last tables entries only.

我有一个'log'对象来捕获一些成功或错误变量,因为websql事务循环遍历查询。循环中有一个循环循环通过数据输入,数据输入是从服务器以对象数组中的对象形式提供的,基本上是表格然后是行。这一切都正常,直到调用sql-query(而不是最终的事务成功。)的内部成功回调。从下面我可以看到,我试图调用函数构建器函数来捕获表名变量,但是在调用返回的函数时它是未定义的。我尝试了很多方法,但我最终只有未定义或最后的表条目。

I have tried to simplify my code below to focus on the issue, some of the code may be messy.I understand why its not available due to asynchronicity but not how to get around it.

我试图简化下面的代码以专注于这个问题,一些代码可能是凌乱的。我理解为什么它不可用由于异步性而不是如何解决它。

addServData = function(data){
    var columns, colCount, rowCount, Q, Qmks, table, 
        rows, dataWatch = {success:{},error:{}};
    /*var tableName; //<-- MOVED THIS INTO LOOP AS THOUGHT WAS THE PROBLEM*/
    oDb.transaction(function(tx){
        for(var key in data){
            var tableName = key;
            table = data[key];
            rows = table.data;
            columns = table.columns;
            colCount = table.colLen;
            rowCount = table.rowLen;
        if(rowCount <= 0) continue;
            ...
        Q = 'BUILD QUERY.... (?,?,?)';

        for(var x = rows.length-1; x >=0; x--){
            var $i = rows.length - (x+1);// <-- INVERT COUNTER
            //sort row object to array in order of colums;
            var row = rows[$i],
                params = utils.sortObjtoArr(row, columns);

            tx.executeSql(Q, params,
                buildCallback(tblName),
                function(tx,error){
                    console.log('error: ', tx,error);
                    dataWatch.error[tblName + '::' + params[0]] = error;
                });
        }

        }


function buildCallback(tbl){ 
                //console.log('buildcallback'+tblName, tx); //PRINTS CORRECT NAME;
            return function(tx,success,tbl){
                console.log('success: ', tx, success, 'tblN:',tbl);//TBL = UNDEFINED;
                dataWatch.success[tbl + '::' + success.insertId] = success.rowsAffected;
                dataWatch.added += parseInt(success.rowsAffected);
            }
            }
    }, function(tx,error){
        console.log(error, dataWatch);

    }, 
    function(tx,success){
        console.log('success', dataWatch); //WORKS

    });
}

1 个解决方案

#1


0  

A standard oversight on my part, confused myself.

我的标准疏忽让自己感到困惑。

Turns out, in my callback builder, i was trying to retrieve the tbl variable from the sql-queries success caller function not from the scope of the callback builder! call back should have been:

事实证明,在我的回调构建器中,我试图从sql-queries成功调用函数中检索tbl变量,而不是从回调构建器的范围中检索!回电应该是:

function buildCallback(tbl){ 
            // return function(tx,success,tbl){ <-- NOT THIS;
            return function(tx,success){ // <-- THIS;
                console.log('success: ', tx, success, 'tblN:',tbl);//tbl = 'TABLE NAME';
                .....
            }
            }

#1


0  

A standard oversight on my part, confused myself.

我的标准疏忽让自己感到困惑。

Turns out, in my callback builder, i was trying to retrieve the tbl variable from the sql-queries success caller function not from the scope of the callback builder! call back should have been:

事实证明,在我的回调构建器中,我试图从sql-queries成功调用函数中检索tbl变量,而不是从回调构建器的范围中检索!回电应该是:

function buildCallback(tbl){ 
            // return function(tx,success,tbl){ <-- NOT THIS;
            return function(tx,success){ // <-- THIS;
                console.log('success: ', tx, success, 'tblN:',tbl);//tbl = 'TABLE NAME';
                .....
            }
            }