Well, JS code errors on Pavlovia

Hi all,
URL of experiment: https://run.pavlovia.org/Alipour/selfpacedreadingtask
https://gitlab.pavlovia.org/Alipour/selfpacedreadingtask
I have put together a self-paced reading task and finally that everything is working perfectly from the Builder view in PsychoPy, I pushed it to Pavlovia to run it online and you guessed it right, I receive JS errors here. Now I fixed some of it by myself but I’m totally new to this, so any help is much appreciated.

Description of the problem: * TypeError: Cannot read property ‘getKeys’ of undefined

ps: the experiment status is private. I couldn’t change it to public but here are the codes:
Begin Experiment

myClock=util.Clock()
win = psychoJS.window;

Begin Routine

words=Sentence.split(' ')
nWords=len(words)
wordNum=0
oldWordNum=-1
phrase=' '
psychoJS.eventManager.clearEvents()
myClock.reset()
win.color = "white"

Each Frame

if wordNum>oldWordNum:
    phrase+=words[wordNum]+' '
    text.text=words[wordNum]
    oldWordNum=wordNum

theseKeys = psychoJS.EventManager.getKeys()
if 'space' in keys:  
    wordNum+=1
    thisExp.addData("Word"+str(wordNum),myClock.getTime())
    myClock.reset()
    if wordNum==nWords:
        continueRoutine=False

Best regards

Should it be eventManager? Please check my 2020 crib sheet.

Your JS clock definition seems to be missing the word new

Thank you for the reply. Yes, check this out

https://psychopy.github.io/psychojs/module-core.EventManager.html
No, it’s not missing anything; it’s the Python code I copied here. Look
ex
routine


After adding the psychoJS prefix, I rewatched part of a tutorial in which the guy was working with JS. That’s why I entered theseKeys that should be obviously omitted.

You are writing code on the Python side that won’t work locally in order to get the correct JS code. I do do this sometimes, but it’s not my recommended method (which is code_JS to make as much correct Python code as possible translate into correct JS code – see my crib sheet).

For example, event=psychoJS.eventManager;

Not psychoJS.EventManager as in your code

Dear @wakecarter,
I did read your crib sheet, but I’ll look into it once more. So I receive this error and I know exactly why.

  • ReferenceError: keys is not defined

Should i just start from scratch and rewrite everything in JS? And could you tell me how to define keys? sorry for asking too many questions. I’ll read the crib sheet for online experiments.

Look at line 6 of Readingcode and tell me why keys isn’t defined in line 7

Well I solved this problem by changing Keys to events:

event=psychoJS.eventManager.getKeys()
if 'space' in event:  
    wordNum+=1
    thisExp.addData("Word"+str(wordNum),myClock.getTime())
    myClock.reset()
    if wordNum==nWords:
        continueRoutine=False

So after that, there was another for thisExp which I changed into psychoJS.experiment.addData, but now it gives me another error for addData!
So I’m gonna ask you, which crib sheet of yours should I be looking at exactly?
Because the online one didn’t contain such information.

The simplest version (which may be all you need) is

code_JS containing


thisExp=psychoJS.experiment;
win=psychoJS.window;
event=psychoJS.eventManager;

in Begin Experiment

However, switching out keys for event (which gets defined in code_JS) is likely to cause issues.

Thank you @wakecarter,
I added these codes to Begin Experiment and deleted event an rewrote Keys instead. Unsurprisingly, I received this error:

  • ReferenceError: keys is not defined

How should I define keys dear @wakecarter if I don’t replace it with event? I do appreciate your help.

keys=event.getKeys()
if ‘space’ in keys:

Thank you! but now

  • TypeError: event.getKeys is not a function

I mean my codes are perfect why?
Begin Exp

myClock=util.Clock()
thisExp=psychoJS.experiment;
win=psychoJS.window;
event=psychoJS.eventManager;

Begin Routine

words=Sentence.split(' ')
nWords=len(words)
wordNum=0
oldWordNum=-1
phrase=' '
event=psychoJS.eventManager.clearEvents()
myClock.reset()
win.color = "white"

Each Frame

if wordNum>oldWordNum:
    phrase+=words[wordNum]+' '
    text.text=words[wordNum]
    oldWordNum=wordNum

event=psychoJS.eventManager.getKeys()
keys=event.getKeys()
if 'space' in keys:  
    wordNum+=1
    psychoJS.experiment.addData("Word"+str(wordNum),myClock.getTime())
    myClock.reset()
    if wordNum==nWords:
        continueRoutine=False

Ps: the color code is not working. I’ll address it in another discussion of mine.

Delete this line

I did

  • TypeError: Cannot read property ‘getKeys’ of undefined

the same error. no, different. But the same element?

Are you using Ctrl Shift R to flush the cache?

Do you have getKeys anywhere other than keys = event.getKeys() ?

No, I’m not. Although I cleared cache when you said so. Still the same error

  • TypeError: Cannot read property ‘getKeys’ of undefined

And no! I don’t have getKeys anywhere else.

Here’s the entire code for my SPR. (and thank you again for the suggestions and responses of yours. I would have gone mad by now if it weren’t for your help.)

Please show a screenshot of where you are defining event.

Are you defining it twice?

I think not. I defined it once in Begin Experiment and there’s another event in Begin Routine - for clearEvents().
1
2
3

If you put x=1 in Begin Experiment and x=2 in Begin Routine then it’s not surprising that x=2 in Each Frame.

Replace line 6 with event.clearEvents()

Okay. Thank you for the suggestion and I did remove line 6, but it’s becoming like a chicken-and-egg problem. Keys defined, events undefined; and the other way around.

  • TypeError: Cannot read property ‘clearEvents’ of undefined

Best regards tho

I think maybe one of the issues is that you are using a single code component both at code_JS for Begin Experiment and Auto for Begin Routine and Each Frame.

I also note that you could have got confused about which line 6 I meant. I meant Begin Routine.

Please could you post screenshots of your code again.