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

**URL:** <https://discourse.psychopy.org/t/typeerror-cannot-use-in-operator-to-search-for-space-in-undefined/12619>\
**Category:** Online experiments\
**Created:** [April 21, 2020, 3:08pm UTC](https://discourse.psychopy.org/t/typeerror-cannot-use-in-operator-to-search-for-space-in-undefined/12619 "2020-04-21T15:08:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![arkadiy](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/arkadiy/32/6068_2.png) [@arkadiy](https://discourse.psychopy.org/u/arkadiy)\
**Post date:** [April 21, 2020, 3:08pm UTC](https://discourse.psychopy.org/t/typeerror-cannot-use-in-operator-to-search-for-space-in-undefined/12619/1 "2020-04-21T15:08:40Z")

</div>

URL of experiment: [https://run.pavlovia.org/arkadiym/oddball\_test89/html](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:

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

```

Automatic JS translation:

```auto
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.

---

<div class="post-metadata">

**Author:** ![dvbridges](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/dvbridges/32/1918_2.png) [@dvbridges](https://discourse.psychopy.org/u/dvbridges)\
**Post date:** [April 21, 2020, 3:27pm UTC](https://discourse.psychopy.org/t/typeerror-cannot-use-in-operator-to-search-for-space-in-undefined/12619/2 "2020-04-21T15:27:08Z")

</div>

@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:

```auto
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:

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

```

---

<div class="post-metadata">

**Author:** ![arkadiy](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/arkadiy/32/6068_2.png) [@arkadiy](https://discourse.psychopy.org/u/arkadiy)\
**Post date:** [April 21, 2020, 3:35pm UTC](https://discourse.psychopy.org/t/typeerror-cannot-use-in-operator-to-search-for-space-in-undefined/12619/3 "2020-04-21T15:35:37Z")

</div>

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