1

I am trying to use a function to read csv file and return it. but unfortunately i got an empty string

function getStations() {

    var final_results = [];

    $.ajax({
        type: "GET",
        url: "CSV.csv",
        dataType: "text",
        success: function (data) {
            var lines = data.split(/\r\n|\n/);

            //Set up the data arrays
            var stations_data = [];

            //var headings = lines[0].split(','); // Splice up the first row to get the headings
            for (var j = 1; j < lines.length; j++) {
                var values = lines[j].split(','); // Split up the comma seperated values
                // We read the key,1st, 2nd and 3rd rows 

                // tmp_data.push(parseFloat(values[0])); 
                //tmp_data.push(parseFloat(values[1]));
                //tmp_data.push(parseFloat(values[2]));
                //tmp_data.push(values[4]);
                stations_data.push(values[4]);
                final_results.push(values[4]);
            }

            console.log("inside:" + final_results.length)
        }
    });

    // Let's process the data from the data file
    console.log("outside: " + final_results.length);
}

the results is as following: console.log results

any suggestions?

Daniel B
  • 8,770
  • 5
  • 43
  • 76
Xiangfeng
  • 11
  • 1

1 Answers1

0

The console.log('outside.. statement gets executed before the ajax has finished. You will have to do some sort of callback for when the final_results is loaded.

function getStations(callback) {

    var final_results = [];

    $.ajax({
        type: "GET",
        url: "CSV.csv",
        dataType: "text",
        success: function (data) {
            var lines = data.split(/\r\n|\n/);

            //Set up the data arrays
            var stations_data = [];

            //var headings = lines[0].split(','); // Splice up the first row to get the headings
            for (var j = 1; j < lines.length; j++) {
                var values = lines[j].split(','); // Split up the comma seperated values
                // We read the key,1st, 2nd and 3rd rows 

                // tmp_data.push(parseFloat(values[0])); 
                //tmp_data.push(parseFloat(values[1]));
                //tmp_data.push(parseFloat(values[2]));
                //tmp_data.push(values[4]);
                stations_data.push(values[4]);
                final_results.push(values[4]);
            }

            callback(final_results)
        }
    });
}

getStations(function (final_results){
    console.log("outside: " + final_results.length);
})
Manuel Otto
  • 6,410
  • 1
  • 18
  • 25
  • thanks for the quick answer, I made the changes: getstations.js:28 Uncaught TypeError: callback is not a function at Object.success (getstations.js:28) at fire (jquery-1.12.4.js:3232) at Object.fireWith [as resolveWith] (jquery-1.12.4.js:3362) at done (jquery-1.12.4.js:9840) at XMLHttpRequest.callback (jquery-1.12.4.js:10311) – Xiangfeng Nov 24 '17 at 19:11
  • you must replace `getStations()` with `getStations(function (final_results) ...)` – Manuel Otto Nov 24 '17 at 19:16