它说数组未定义?

时间:2013-08-01 13:54:01

标签: javascript arrays split return undefined

在我的代码中,我通过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;
}

所以我希望有人知道答案吗?关于我的代码和/或帖子格式的其他批评也是受欢迎的:)

1 个答案:

答案 0 :(得分: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 
}