psychopy.org | Reference | Downloads | Github

TypeError: Cannot read properties of undefined (reading 0)

Hi,

URL of experiment: Sign in · GitLab

Description of the problem: After running one block of trials online, I get the following error message when trying to continue to the second block: TypeError: Cannot read properties of undefined (reading 0)

Similar issues here and here suggest that some variable while working in Builder locally, isn’t fully defined in JS. I have not defined anything using var.

The issue is likely around trialRepeats = [5, 6, 5, 6], defined at the beginning of the experiment. This is then passed into nReps of the trial loop as trialRepeats[-i] (where i is increased with each block).

My goal was to vary the number of trials per block, and this solution works well locally, so I’m not totally clear on what exactly isn’t properly defined.

Any help is greatly appreciated.

Hi @kaylap,
the internet tells me that negative indexing in javascript returns undefined, so I would first change the python code that is translated to javascript to index from the beginning of the list - not from the end (or compute the index directly, like len(trialRepeats) - i and not rely on negative indexing).
I’d also make sure that trialRepeats list is not redefined anywhere in your experiment (by accident) to something other than a list. I had a similar error in pavlovia recently and the cause was overwriting the list variable with something else.

Thanks for your reply! I tried changing both the name of trialRepeats to ensure I hadn’t missed anything else with the same name and also changing the code to not reference a negative index, but I’m still getting the same error but before the first block now.

If there’s an easier way to randomize or vary the number of trials per block, that would also be great.

Please could you show what your flow and loops currently loop like?

You can’t change the nReps of a loop once it has started, but you can definitely set it as a variable so long as the variable is defined before the loop starts.

Turning the value for nReps to a variable then manipulating that variable worked - thank you!