I have the following script and it has an interesting interaction.
$(document).ready(function () {
$.get('data/experiments.json', function (data) {
trialsData = data.experiments;
console.log(trialsData);
console.log(data);
currentTrial = 0;
totalNumberOfTrials = data.experiments.length;
$('.js-expt-num-trials').text(totalNumberOfTrials);
data.experiments.push(headers);
next();
pid = ACPToolKit.getCurrentParticipantId();
})
});
When I console.log(data)
, it returns the actual and correct data which is
{
"experiments": [
{
"technique": "AUTOCOMPASTE",
"granularity": "sentence",
"data_file": "data/texts.json",
"stimuli": "What is acceptor atoms?"
},
{
"technique": "AUTOCOMPASTE",
"granularity": "paragraph",
"data_file": "data/texts.json",
"stimuli": "What is AC coupling? Circuit that passes an AC signal while blocking a DC voltage."
},
{
"technique": "AUTOCOMPASTE",
"granularity": "phrase",
"data_file": "data/texts.json",
"stimuli": "number of positive charges"
},
{
"technique": "TRADITIONAL",
"granularity": "sentence",
"data_file": "data/texts.json",
"stimuli": "What is acceptor atoms?"
},
{
"technique": "TRADITIONAL",
"granularity": "paragraph",
"data_file": "data/texts.json",
"stimuli": "What is AC coupling? Circuit that passes an AC signal while blocking a DC voltage."
},
{
"technique": "TRADITIONAL",
"granularity": "phrase",
"data_file": "data/texts.json",
"stimuli": "number of positive charges"
}
]
}
However, when I console.log(trialsData)
or even console.log(data.experiments)
for that matter, it returns undefined. Needless to say that data.experiments.length
or trialsData.length
also returns the error Uncaught TypeError: Cannot read property 'length' of undefined
I have no idea what I'm doing wrong here. I'm pretty sure that data.experiments
is correct syntax