-1

在我的代码中,我通过 for 循环中的另一种方法填充数组。我已经验证了数组实际上已经填充了正确的信息,但是当我尝试返回并使用它时,它说它是未定义的,但是为什么呢?

代码:

function Data(name, csv){
    this.dataTable = null;
    this.createDataTable(name, csv);
}

Data.prototype.createDataTable = function(name, csv){
    dataTable = persistence.define(name, {
        Tank1: "INT",
        Tank2: "INT",
        Tank3: "INT",
        Tank4: "INT",
        Helicopter: "INT",
    });
    this.fetchData(csv);
};

Data.prototype.fetchData = function(csv){
    jQuery.get(csv, function(data) {
        var result = $.csv.toArrays(data);
        result.shift();
        result.forEach(function (r) {
            var joinedResult = r.join();
            fillDB(joinedResult);
        });
    });

    var test = getData();  <--- This is where I am testing the code.
    var joinedResult = testlolol.join();
    var splitData = joinedResult.split(";");
    alert(splitData);
};

//This function will fill the database using the parsed array line from the csv file.
function fillDB(DataArray){
    //Wipe local database clean
    persistence.reset(function(){
        //Write schema
        persistence.schemaSync(function(){
            //Add values from the array to their matching columns.
            var data = new dataTable();
            var splitData = DataArray.split(";");
            data.Tank1 = splitData[0];
            data.Tank2 = splitData[1];
            data.Tank3 = splitData[2];
            data.Tank4 = splitData[3];
            data.Helicopter = splitData[4];
            persistence.add(data);
            persistence.flush();
        });
    });
}

function deleteData(column, operator, value){
      persistence.flush(function(){
       dataTable.all().filter(column,operator , value).destroyAll();
      });
}

function addData(Tank1, Tank2, Tank3, Tank4, Helicopter){
        var data = new dataTable();
        data.Tank1 = Tank1;
        data.Tank2 = Tank2;
        data.Tank3 = Tank3;
        data.Tank4 = Tank4;
        data.Helicopter = Helicopter;
        persistence.add(data);
        persistence.flush();
}

function getData(){
    var newDataArray = new Array();
    dataTable.all().list(function(tasks){
        tasks.forEach(function (r) {
            var tTank1 = r.Tank1;
            var tTank2 = r.Tank2;
            var tTank3 = r.Tank3;
            var tTank4 = r.Tank4;
            var tHelicopter = r.Helicopter;
            newDataArray.push(tTank1, tTank2, tTank3, tTank4, tHelicopter);
            tempTest(newDataArray); 
        });
    });
}

function tempTest(array){
    var newDataArray = new Array();
    newDataArray.push(array);
    alert(newDataArray);   <-- these contain the correct values
    return  newDataArray;
}

所以我希望有人知道这个问题的答案吗?也欢迎对我的代码和/或帖子格式提出其他批评:)

4

1 回答 1

2

您没有返回任何值

function getData(){
    var newDataArray = new Array();
    dataTable.all().list(function(tasks){
        tasks.forEach(function (r) {
            var tTank1 = r.Tank1;
            var tTank2 = r.Tank2;
            var tTank3 = r.Tank3;
            var tTank4 = r.Tank4;
            var tHelicopter = r.Helicopter;
            newDataArray.push(tTank1, tTank2, tTank3, tTank4, tHelicopter);
            tempTest(newDataArray); 
        });
    });
    return newDataArray 
}
于 2013-08-01T13:57:15.377 回答