Error: Cannot use ‘in’ operator to search for ‘undefined’ in undefined

URL of experiment: dscrimination [PsychoPy]

Problem description: the Javascript code is losing almost all the definition codes?(sorry I am not sure whether it is the name) (var ****) after I used the translation tool on psychopy, while the python code is totally ok and the experiment can run on my laptop perfectly. I added all the variables myself in the Javascript. However, the problem now is TypeError: Cannot use ‘in’ operator to search for ‘undefined’ in undefined . I have consulted many related topics but I can’t find any solution.

Could anyone help me with this issue? I almost run out of time for my master thesis. Thank you in advance!

https://discourse.psychopy.org/uploads/short-url/uHgcekThGqyEw5GBWOKJZHfsJVk.psyexp

// *opt4_2* updates
    if (t >= 0.0 && opt4_2.status === PsychoJS.Status.NOT_STARTED) {
      // keep track of start time/frame for later
      opt4_2.tStart = t;  // (not accounting for frame time here)
      opt4_2.frameNStart = frameN;  // exact frame index
      
      opt4_2.setAutoDraw(true);
    }

     var _pj;
     function _pj_snippets(container) {
         function in_es6(left, right) {
             if (((right instanceof Array) || ((typeof right) === "string"))) {
                 return (right.indexOf(left) > (- 1));
             } else {
                 if (((right instanceof Map) || (right instanceof Set) || (right instanceof WeakMap) || (right instanceof WeakSet))) {
                     return right.has(left);
                 } else {
                     return (left in right);
                 }
             }
         }
         container["in_es6"] = in_es6;
         return container;
     }
    _pj = {};
    _pj_snippets(_pj);
    keys = psychoJS.eventManager.getKeys();
    if (keys.length) {
        if (_pj.in_es6("left", keys)) {
            slider_2.markerPos = (slider_2.markerPos - 1);
        } else {
            if (_pj.in_es6("right", keys)) {
                slider_2.markerPos = (slider_2.markerPos + 1);
            }
        }
    }
    psychoJS.experiment.addData("Rating", slider_2.markerPos);

I think that there are probably issues with the start conditions for your keyboard components.

next_2

len(_choose_2_allKeys) > 0

_choose_2_allKeys doesn’t exisit and choose_2.keys is undefined until the first choose_2 key is pressed.

I don’t know if $(‘g’ in choose_2.keys) works as a stop condition locally but online I’d expect to need to define a variable in an auto code component and then use that.

You have two keyboard components and event.getKeys in the same routine, which is likely to result in unwanted behaviour.

[Thread continued from Stuck on initialising the experiment page]

Thank you for your message!

Do you mean the code component in Builder?

Yes

Thank you for your prompt reply.
I have added the following code below “choose_2” keyboard component and deleted the stop condition leaving it black.
However, the python or js codes didn’t change accordingly, still showing the previous version (len(_choose_2_allKeys) …)

if choose_2.status == STARTED:
    choose_2_Stop = ('g' in choose_2.keys)
    if choose_2_Stop:
        choose_2.tStop = t  # not accounting for scr refresh
        choose_2.frameNStop = frameN  # exact frame index
        win.timeOnFlip(choose_2, 'tStopRefresh')  # time at next scr refresh
        choose_2.status = FINISHED