【问题标题】:Undefined when trying to access an array inside nested queries尝试访问嵌套查询中的数组时未定义
【发布时间】:2018-05-18 00:01:01
【问题描述】:

我知道这个问题与回调有关,但我就是无法解决。经过几个小时的研究,我决定发布它。 这是我的代码:

function getTables(callback)
{ 
connection.query('show tables', function (error, allQueries, fields) {
        if (error) throw error;
        else{
            for(var i=0;i<allQueries.length;i++){
                tables.push(allQueries[i].Tables_in_ProyectoFinal);
            }
            callback(tables);
        }
});
}

getTables(function (tables){
    for(var i = 0;i < tables.length; i++){
        connection.query('show columns from ' + tables[i], function (error, allColumns, fields) {
                if (error) throw error;
                else{
                    for(var j = 0;j < allColumns.length; j++){
                        //columns.push("select " + allColumns[j].Field + " from "+ tables[i]);
                        console.log("Column: "+allColumns[j].Field+" from table: "+ tables[i]);
                    }
                }
        });
    }


});

我得到这个结果:

Column: peliculaId from table: undefined
Column: titulo from table: undefined
Column: ano from table: undefined
Column: personaId from table: undefined
Column: nombre from table: undefined
Column: apellido from table: undefined

【问题讨论】:

    标签: javascript mysql node.js


    【解决方案1】:

    我怀疑allQueries[i].Tables_in_ProyectoFinal 会以undefined 出现。检查该属性是否拼写正确以及它是否存在于 allQueries 数组中的每个对象上。

    【讨论】:

      【解决方案2】:

      您遇到此问题的原因是因为i 的值已经比表数组的长度大 1。

      connection.query 是异步的。这意味着带有表的 for 循环将运行,而不是等待回调完成。 我敢肯定,如果你把console.log(i)j 放在for loop 中,你会得到i = tables.length

      看下面这个例子

      function myAsyncFunction(tableName, callbackFunction) {
        setTimeout(function () {
          console.log('table name', tableName);
          callbackFunction(null, ['column1', 'column2']);
      
        }, 100);
      }
      
      function getTables(tables) {
        for (var i = 0; i < tables.length; i++) {
          myAsyncFunction(tables[i], function (error, allColumns) {
            for (var j = 0; j < allColumns.length; j++) {
              console.log('i is', i);
              console.log('tables', tables[i]);
            }
          })
        }
      };
      
      getTables(['table1', 'table2']);

      i 的值为 2

      【讨论】:

        【解决方案3】:

        非常感谢您的回答。我是这样解决的:

        function getTables(callback)
        { 
        connection.query('show tables', function (error, allQueries, fields) {
                if (error) throw error;
                else{
                    for(var i=0;i<allQueries.length;i++){
                        tables.push(allQueries[i].Tables_in_ProyectoFinal);
                        columns.push("SELECT *  FROM " +allQueries[i].Tables_in_ProyectoFinal);
                    }
                    callback(tables);
                }
        });
        }
        function getAll(table,callback)
        { 
        connection.query('show columns from ' + table, function (error, allColumns, fields) {
                if (error) throw error;
                else{
                    for(var j = 0;j < allColumns.length; j++){
                        //console.log("Column: "+allColumns[j].Field+" from table: "+ table);
                        columns.push("SELECT "+allColumns[j].Field+" FROM "+ table)
                    }
                    callback(columns);
                }
        
        });
        }
        
        getTables(function (tables){
            for(var i = 0;i < tables.length; i++){
                getAll(tables[i],function(columns){
                    console.log(columns);
                });
            }
        });
        

        我用回调构建了两个函数

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-08-22
          • 1970-01-01
          • 1970-01-01
          • 2016-10-08
          • 2021-08-02
          • 2016-07-08
          相关资源
          最近更新 更多