Currently, I am utilizing AJAX to parse a CSV file into an Object of Arrays. However, I am encountering an issue where the data seems to be successfully read and stored in the Object but when I try to access it at deeper levels, I receive undefined messages. It appears that everything is present in the Object upon initial inspection. My suspicion is that this issue may be related to timing, especially since I am dealing with significant amounts of data.
JS
$(document).ready(function () {
var data = fetchCSVData();
displayTable(data);
});
function fetchCSVData() {
var data = {"columns":[], "rows":[]};
$.ajax({
url : 'assets/php/readCSV.php',
type : 'POST',
success : function (csv) {
var colCount = csv[0].length,
rowCount = csv.length;
for (var c = 0; c < colCount; c++) {
data['columns'][c] = csv[0][c];
}
csv.splice(0,1);
for (var r = 0; r < rowCount; r++) {
data['rows'][r] = csv[r];
}
},
error : function () {
alert("Error: Unable to read the CSV file. Please try again.");
}
});
return data;
}
function displayTable(data) {
console.log(data); // Successfully displays all data.
console.log(data['columns']); // Displays columns data as expected
console.log(data['columns'][0]); // Returns as undefined
}
Snippet from first console log
Object { columns=[0], rows=[0]}
columns ["EXPERIMNT CODE", "EXPERIMNT_NAME", "VarCode", 12 more...]
rows [["H1225", "COP - Show star rating a...ting in the price panel", "H1225:001.000", 12 more...], ["H1225", "COP - Show star rating a...ting in the price panel", "H1225:001.001", 12 more...], ["H1225", "COP - Show star rating a...ting in the price panel", "H1225:001.002", 12 more...], 4873 more...]
Snippet from second console log
[]
0 "EXPERIMNT CODE"
1 "EXPERIMNT_NAME"
2 "VarCode"
3 "VarName"
4 "Version Number"
5 "Reporting Range Start Date"
6 "Reporting Range End Date"
7 "Status"
8 "Transaction Date"
9 "EXPERIMNT TEST ID"
10 "Test Manager"
11 "Product Manager"
12 "Pod"
13 "Record_Update_Datetm"
14 "Insert_datetm"
The third log returns undefined despite the presence of data indicated in the second snippet.
Any insights on what might be causing this discrepancy?