psychopy.org | Reference | Downloads | Github

Variable not defined

URL of experiment: https://run.pavlovia.org/psychunsw/forpsylab-anita-trinh-str-3-exp-2-v2/html/
JS Code: https://gitlab.pavlovia.org/psychunsw/forpsylab-anita-trinh-str-3-exp-2-v2/blob/master/html/Strand3Exp2_Draft2_OnlineOnly.js

Hi all! So the Builder version of my experiment runs just fine, but once I had uploaded the experiment onto Pavlovia, I wasn’t able to run my trials due to the error “this_loop_number not defined” (this_loop_number is a variable I defined myself in a Code Component). In my Code Component, the variable is defined at the beginning of the experiment, but the error for this_loop_number only appears once it reaches the conditions loop.
This experiment is being run at version 3.1, and I currently have PsychoPy 2020.1 installed on my computer.

I’ve tried doing the following things, none of which have worked:

  • Changed all my Code Components (including the one defining this_loop_number) to “Auto > JS” from “Py”, then syncing the experiment to Pavlovia, and;
  • Changing the code to say “var this_loop_number = 0;” rather than “this_loop_number = 0;”

Any help would be appreciated. Thank you!

Please go to your repository, then settings -> general -> permissions, set access to public. I can’t currently see the code.

This is possibly related: Begin routine from the builder doesn't work online
As is this: Undefined parameters in Pavlovia

either there’s an issue with “begin experiment” code components, or there’s some kind of sync error. Either way, it would be helpful to have a look at your code.

Hi Jonathan,

Thank you for your response! I have set the code to public.

I think it’s a scope issue. “this_loop_number” is defined in the begin experiment function, and then referenced inside another function that is not called from inside it. Scope is kind of a complex concept if you aren’t familiar with it, but the bottom line is that right now it’s only being defined for other things within the “begin experiment” code block, and you need it to be available globally.

The solution is actually to delete the word “var” from in front of it and just put this in your begin experiment code:

this_loop_number = 0;

This should declare it as an implicit global variable (see https://www.w3schools.com/js/js_scope.asp)

I also see something else which will cause trouble which is the line after that:

import * as random from 'random';

This doesn’t work in JS. In fact, no import statement from a code component will work in JS right now. The shuffle function you will have to re-create manually, but this is easy to do with the tools available. There’s an example function that works very well here: Reference error: shuffle is not defined

1 Like

Thank you Jonathan, that worked out! Also, thank you for the pointer about the “random” function - I’m learning some preliminary Javascript to find out which Python functions that I’ve used in my code components need to be replaced with Javascript ones.

Much appreciated!