| Reference | Downloads | Github

Uncaught (in promise) TypeError: Cannot read properties of undefined (reading '0')

URL of experiment: FYP [PsychoPy]

Description of the problem:

When running the experiment on Pavlovia, before the block I get the error:

TypeError: Cannot read properties of undefined (reading ‘0’)

Using the console it shows:

The error is on the first line of this:

this.thisIndex = this._trialSequence[this.thisRepN][this.thisTrialN];
				this.thisTrial = this.trialList[this.thisIndex];
				this.ran = 1;
				this.order = this.thisN;

I do use code to specify the number of reps in the final block using $choiceBlockReps , which is defined earlier in the experiment, is this likely to be where the issue is?

Thank you


Try removing the $ from choiceBlockReps

The $ is hard coded into the nReps bit, not part of the separate code. Thought we’d got it then!

The code for my reps is

choiceBlockReps = len(imgL)

Could that be causing the issue? Though it follows what was recommended to me by Becca and her Pavlovia upload runs fine using len(list) as the value for reps.

Is your loop pointing at a spreadsheet? Please could you show a screenshot of its definition?

Try print('choiceBlockReps',choiceBlockReps) just after the above line of code to check it is being set correctly in the console.

The loop does not have a spreadsheet, the images are chosen from earlier code.


Adding the print code gave me:

choiceBlockReps 59

which is accurate for the choices I made during that run.

That all looks fine. Are you using trialsChoice for anything else?

It’s used in the image call up:


It’s also referenced in the code to save the extra data

thisExp.addData('Image Left', imgL[trialsChoice.thisN])
thisExp.addData('Image Right', imgR[trialsChoice.thisN])

I have now also made the git repo public Jake Diggins / IM_FYP2122 · GitLab

I have managed to finally fix this issue.

I had to move the choiceBlockReps code from “begin routine” to “end routine” and it works perfectly.

Is this a quirk of JS? As it was able to accept the variable fine from where it was in Python.

I tend to have most of my code in Begin Routine.

It is also good practice to move the code component to the top of the routine, so that Begin Routine is processed before any Each Repeat components.

One quirk of JS is that the first time a variable is assigned, it needs to be outside any brackets.

For example:

if random() > .5:
     condition = 1
     condition = 2

will fail with a condition not defined error but

condition = 2
if random() > .5:
     condition = 1

is fine.