尝试访问嵌套查询中的数组时未定义

时间:2018-05-16 18:20:11

标签: javascript mysql node.js

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

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

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

connection.query是异步的。这意味着for循环表将运行,而不是等待回调完成。 我相信如果您将console.log(i)放入for loop j,您将获得i = tables.length

查看下面的示例

&#13;
&#13;
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']);
&#13;
&#13;
&#13;

i的值为2

答案 2 :(得分:0)

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

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);
        });
    }
});

我使用回调构建了两个函数

相关问题