| Reference | Downloads | Github

Nameerror when using Javascript on builder

If this template helps then use it. If not then just delete and start from scratch.

OS (e.g. Win10):
PsychoPy version (e.g. 3.2.4.x):
Standard Standalone? (y/n) If not then what?:
What are you trying to achieve?:
familiarizing myself with psychopy builder and online experiment coding
What did you try to make it work?:
My experiment is super simple right now. I have a “var num_learn = 12” variable declared in Begin Experiment. For my first learn trials experiment, I refer to num_learn using $num_learn. But when I run it, I keep getting the “NameError: name ‘num_learn’ is not defined” message.

I used Python previously and there was no problem. I wrote the same code in JS before I pushed it online. I got the same “ReferenceError” when I tried to run it online. So I went back to the builder and removed the Python code (when I had both codes, there was no problem) and worked on the JS code. But I keep running into this problem and now I have no idea what’s happening.

What specifically went wrong when you tried that?:
Include pasted full error message if possible. “That didn’t work” is not enough information.

hi @CatK, try removing the “var” declaration. PsychoPy finds all your variables used in the experiment and declares them globally using var, but if you do this yourself you restrict scope to that part of the experiment (e.g., beginning of the experiment)

I removed the “var” declaration" but still got the same nameerror. Here are some screenshots of my codes and settings. Is there anything else that I can try/change?

Thanks, would you mind sharing the URL for the task?

I deleted the original depository. I tried to commit it again but it doesn’t show on my pavolovia page.

Is there any demo project you recommend that involves using js code snippet? Thanks!

I had some problem pushing it online (i deleted the original online repository).The attachment is the psyexp file. Thank you so much!

experiment_testing 1.psyexp (16.1 KB)

Ok, well I uploaded this task ok, but got a ReferenceError because item_blast does not exist. Is there a conditions file I should have? Or should there be another array called item_blast?

Sorry for that. I changed the variable name a bit and clearly didn’t change all of them. Here’s the updated one. I’m so sorry for the inconvenience.experiment_testing 1.psyexp (16.1 KB)

Thanks, this also compiles correctly, and the task runs just fine. Can you please try refreshing your browser cache to make sure that you are running the most up to date script? On Windows, you would open your task in your browser from Pavlovia and press “Shift + F5”.

Which version of psychopy do you use?

Somehow I’m not able to open psychopy app on my computer now, which is very weird.

Which version of psychopy do you use?

Somehow I’m not able to open psychopy app on my computer now, which is very weird.

I am using the latest version (3.2.4). If you cannot open the app, try the following , it usually means you have to reset your app data:

Thank you. I did what you recommended and now I can open the app now. But when I run it locally on my computer, it still gives me the message before the experiment gets to the trial section: File “C:\Users\cathe\Downloads\experiment_testing”, line 462, in
trialsLearn = data.TrialHandler(nReps=num_learn, method=‘random’,
NameError: name ‘num_learn’ is not defined

Anything else that you’d recommend me to do? Mine is also 3.2.4 version

This error is because you have not got the equivalent Python code that defines the “num_learn” variable. In your image above, you have a code component with code type of “both”, showing the JS on the right, but no equivalent Python on the left. You just need to translate the JS to Pytho, and that error should be solved.It really depends on what you want to do though, if you only want to run it locally, use Python.

Thank you! I had Python code but removed it because it wasn’t working online and I was trying to debug. I now try to run it online. But although I was able to push it to gitlab, it doesn’t show on my dashboard. How can make it show on dashboard?

Sorry for the inconveniences. It’s my first time dealing with psychopy and i need to create an experiment that can be put online. Thank you for all your help!!

Thanks @CatK, looks like this was an issue but was very recently fixed:

Would you mind recreating your project in another repository. To do this, you will first need to delete the .git folder in your project directory. It is a hidden folder though (preceded by “.” - as in .git), so you may need to set your folder options to view hidden files and folders.

It finally works now! (Online too!) Thank you so much!!!