After converting a CSV file to JSON and adding custom headers for some mysterious reason, my data now appears like this:
https://i.sstatic.net/H7Ls0.png
Within my JavaScript file, I have the following line of code:
console.log(jsonData.theData[0].symbol);
However, when I try to execute it, I receive the error message:
admin.js:46 Uncaught TypeError: Cannot read property '0' of undefined
From my perspective, it seems like '0' is defined. Oddly enough, when I run the same command directly in the browser console, it works perfectly fine.
https://i.sstatic.net/Thyi6.png I have also attempted the following:
JSON.stringify(jsonData.theData[0])
Unfortunately, this results in the same error. Any thoughts on why this might be happening?
Below is the content of my JavaScript file:
var jsonData = {};
var theData = [];
document.getElementById("fileToRead").addEventListener("change",function() {
var input = document.getElementById("fileToRead");
for(var i = 0; i < input.files.length; i++){
var files = input.files[i];
Papa.parse(files, {
header:false,
dynamictyping:true,
complete:function(results){
var input = results.data;
input.forEach(function(input){
jsonData.theData = theData;
var singleEntry = {
"symbol" : input[0],
"date" : input[1],
"open" : input[2],
"high" : input[3],
"low" : input[4],
"close" : input[5],
"volume" : input[6]
}
jsonData.theData.push(singleEntry);
})
//console.log (jsonData);
// document.getElementById("editor").innerHTML = JSON.stringify(jsonData);
} // End Complete - Callback
}); // End PapaParse
} // End for loop
console.log(jsonData);
});
An update...
Upon adding some console.logs, here's what was revealed: