| Reference | Downloads | Github

Pavlovia javascript isn't declaring my variables from Psychopy .py code

URL of experiment:

Description of the problem: I’ve been trying to run my experiment online and currently I am on pilot mode. However, it keeps giving me errors for undefined variables, ranging from “text” to “clock” and to anything else (a new routine). I have tried to delete the project and upload it again, but it still doesn’t work.

How can I fix this? I have 4000 lines of code and I don’t want to do this line by line.

Please help me!

Your repository is not currently public, so we can’t check it. You can go to settings -> general -> permissions to make it public.

Is this something you created in the builder or hand-coded?

Hello Jonathan,

I have just made it public now.

I made it in the builder but I have also inserted codes using the built-in code thing.

At a glance, and to be clear this was a quick once-over, I think the general problem might be a JS scope issue. I see some “var” statements in your code components. See here: Variable not defined

Basically, JS scope behaves weirdly. In a code component, you don’t want to use “var”, because then the variable will only be defined for that specific part of the code component (e.g., begin routine), not even the whole trial. If you omit “var”, JS recognizes it as global and you can reference it wherever you want.

Hi Jonathan,

I tried to define the variables because it seems to be the problem for all of my variables. Do you know what kind of a global code could help with this?

I have re-uploaded the project again from my original experiment, another problem is that import {choice} from ‘random’ doesn’t work in js code.

Your repository seems to be private again.

Import statements don’t work in code components online, it’s just not something JS can handle right now and probably not ever. Once I have another look at the code I might have a better idea of how to work around that limitation. JS has some decent randomization built into it that you can use without import statements.

I have just made it public again.

Thanks so much!

So it looks like “choice” just takes an element from a list at random. That’s easy enough to do with javascript. I’ll use the first target trial code as an example. The simplest solution is this:

n1target = letters[Math.floor(Math.random() * letters.length)];

But looking at that code block as a whole, you might want this:

letters = ["B", "C", "D", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "V", "W", "X", "Z"];
// Generate a random integer in range 0-length of the letters array
randomIndex = Math.floor(Math.random() * letters.length); 
// Get the item at that index
n1target = letters[randomIndex];
// Delete one item at that index

Note that I’ve removed the word “var” and just defined the “n1target” and “letters” variables outright. That tells JS that these should be treated as global variables, so they can be accessed from outside the “begin routine” function. (Routines are made up of several different functions, so to reference a variable in both “begin routine” and “each frame” you need to make sure the variable is global.)

Hi Jonathan,

Thank you so much for the code. I managed to make it run for the first block. I am very much bothered by the amount of variables I have to define (reference errors). Currently, one of the reference errors persists even though I have defined it.

Another problem in which I have read from other threads is that I cannot use my rating scale.

Apparently, if my original code has problems, .py code cannot be uploaded online successfully, to which it will contain many .js errors (like mine). However, I couldn’t figure out where the problem is because when I run the study offline, it seems to be fine. Do you happen to know why my python code couldn’t transfer to Pavlovia successfully?

3back_demo_Diana.psyexp (137.5 KB)

There’s still a couple of import statements in the “Begin experiment” tabs of a couple of code elements, notably instr1_1. I would also not define “msg” in the “begin experiment” tab multiple times (right now it’s in several feedback trials).

However, the most informative thing would be to see the error messages it throws when you try to compile it. If you look in the running window when you try to create the Pavlovia repository, it should give you error messages that tell you where it’s having problems.