psychopy.org | Reference | Downloads | Github

TypeError: Cannot use 'in' operator to search for 'space' in undefined

URL of experiment: https://run.pavlovia.org/arkadiym/oddball_test89/html

Description of the problem: I am attempting to end a loop if participants press “space” and repeat it if they press “r”. I have seen some issues with ending loops listed on this site, but nothing that seems to apply to this case. When participants reach the end of instructions, they are instructed to press spacebar, this works in psychopy, but I get the following error in Pavlovia:

“TypeError: Cannot use ‘in’ operator to search for ‘space’ in undefined.”

I put the following code into “Begin Routine”, “Each Frame”, and “End Routine”:

Python:

if 'space' in key_resp_11.keys:
    trials_7.finished = True

Automatic JS translation:

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);
if (_pj.in_es6("space", key_resp_11.keys)) {
    trials_7.finished = true;
}

FYI- my loop is set to 999 reps, and should end only if participants press the “space”. I would love to hear any suggestions for fixing this on Pavlovia.

@arkadiy, I think the issue is that the test for “space” is being done before a keypress, which fails, so replace the JS code with the following, but remember to change the name of the keyboard. Also, use trials to end loops, rather than your actual loop name:

if (((key_resp.keys !== undefined) && (key_resp.keys === "space"))) {
    trials.finished = true;
}

If that doesn’t work, because your responses are being saved in a list (i.e., save all keypresses), then:

if (((key_resp.keys !== undefined) && (key_resp.keys.indexOf("space") > -1))) {
    trials.finished = true;
}

The first suggestion worked. Thank you for your help as always!