| Reference | Downloads | Github

Study works fine in builder but not online (objects on screen, code components)

URL of experiment:

Description of the problem:
(1) my objects have disappeared, there should be objects popping up one at a time for a few seconds at a time against the background grid. I have a similar task where the objects pop up fine.

(2) when I get to the phase which has code components in it I get and that’s as far as it will go:
TypeError: Cannot read property ‘xloc’ of undefined

Best wishes,

Well, know that online experiments are ran using JS in the code components, it does not read regular Psychopy code (as the desktop version), so if you are using an older version of Psychopy that might be the problem, you would need to translate it to JS. However, if you use the newest version 2020.1 it has an auto-translation feature. Try that and see how it works. Other option is to manually translate your code snippets and open your code component window, selec code: BOTH and write the translation in the right column.

Hi Laura,

Thanks for getting back to me. I’m using the latest version and used py > autojs.

I have no idea how to manually translate to js - I spent a lot of time changing my python code so that it would work with auto js (I had a previous working version in psychopy which didn’t translate well).

Best wishes,


Can anyone help me over the line here? I’ve done a bunch of googling but with no java training i’m just wasting my time I think.

Much appreciated,

@TomH, looks like the issue is that you have declared the position variables from your conditions file, in a code component. This, I think, has somehow interfered with how the trialhandler is declaring your position variables. To fix, remove your xloc and yloc declarations from your code component.

Thanks -

I tried removing xloc and yloc declarations and then it started complaining about the ‘object’ declarations - are you saying that I can’t refer to anything at all in my conditions file?

So have I to basically recreate these variables (xloc, yloc, object) in my code component, give them a new name and use them for the code component?

If not, could you please specify what you mean?

Best wishes,

The only change you will need to make is to the first routine, where you define xloc and yloc at the beginning of the experiment, in the code component. By removing these, you do not interfere with those variables defined in your conditions file. There is also another change that you might want to make. In another code component, you overwrite xloc and yloc with new values, which are fed to your polygon stim. Change the name of the variables from xloc and yloc, to something different from that in your conditions file, and use these values to set your polygon location (if that is what you planned).

Is this what you mean?
if so then it still doesn’t work - when I remove all of these I still get the error for ‘object’.

Note - I now have my objects back in the exp_phase, its just the “trial > feedback” loop that doesnt work

I just removed the polygon - it isn’t part of the experiment its just easy for testing where the variables are meant to be.

Best wishes,

No, in your first routine of the experiment, you had a code component called code_3, which appears to have been removed now. That was causing the issue, because it had the variables xloc and yloc variables defined at the beginning of the experiment. Removing these fixed the experiment for me. I can send you the version I used, if you would like.

Take a look at this URL, to see the changes working:

Edit: Here is the Builder file for comparison

OL1.psyexp (55.7 KB)

the experiment still doesn’t work in the URL you sent me

  • when you get to the active learning phase “Trial - Feedback” it starts giving the errors - the initial exposure phase is working fine.

Yes If you get it going I would love you to send me it over.

Best wishes,

Ok I see, this may be a different error to the one posted, which appears to be fixed.

its this error from the original post that is causing me the trouble.

Best wishes,

If you can make a minimum working example to recreate the error, with only single trials for each loop, allowing me to skip all the way to the error quickly, then post the pilot URL, I can try and debug it from here. I do not have easy access to my working version of your task at the mo.

Here is a minimum working example.

Thank you very much.


Thanks, its ok I switched machines. The issue was the use of trials.thisTrial['xloc'] in your code component, and other similar expressions. This is not necessary, as you have access to each variable from the current trial just by using the variable name, so trials.thisTrial['xloc'] becomes xloc. To fix, remove any uses of xloc = trials.thisTrial['xloc'] and similar expressions, and just replace with the variable name you are trying to get e.g., xloc.

Once this issue is fixed, you will have another issue, because Pythons range function does not exist in JS, you can write your own though. Add the following code (see source) to a JS “Begin Experiment” tab of a code component - (note, keep auto-translate off for this bit - use code type of “Both”):

function range(start, stop, step) {
    if (typeof stop == 'undefined') {
        // one param defined
        stop = start;
        start = 0;

    if (typeof step == 'undefined') {
        step = 1;

    if ((step > 0 && start >= stop) || (step < 0 && start <= stop)) {
        return [];

    var result = [];
    for (var i = start; step > 0 ? i < stop : i > stop; i += step) {

    return result;

Because the range function is from stack overflow, you will want to thoroughly check your results, to make sure things like the range function are giving you accurate results. Here is a working example of your task with only 1 trial. The Builder file is attached below.

OL1_DB.psyexp (56.2 KB)

So this works in python, completely taking out the need for a range function (this range function approach didn’t fix it):

End Routine:

    if (mouse.x[0] >= xloc-150) and (mouse.x[0] <= xloc+150):
        if (mouse.y[0] >= yloc-150) and (mouse.y[0] <= yloc+150):
            correct = True
            correct = False
         correct = False
    if correct == True:
        obs_dict[image] +=1
    elif correct == False:
        obs_dict[image] = 0

and converts to this in java:

if ((skipThisTrial !== true)) {
    if (((mouse.x[0] >= (xloc - 150)) && (mouse.x[0] <= (xloc + 150)))) {
        if (((mouse.y[0] >= (yloc - 150)) && (mouse.y[0] <= (yloc + 150)))) {
            correct = true;
        } else {
            correct = false;
    } else {
        correct = false;
    if ((correct === true)) {
        obs_dict[image] += 1;
    } else {
        if ((correct === false)) {
            obs_dict[image] = 0;

but it STILL isn’t working in the browser, any idea why? could it be something to do with pixels online or something?

Edit: pixels seem fine