# TypeError: Cannot read property 'concat' of undefined

**URL:** https://discourse.psychopy.org/t/typeerror-cannot-read-property-concat-of-undefined/22734
**Category:** Online experiments
**Tags:** online, pavlovia
**Created:** [May 3, 2021, 10:15am UTC](https://discourse.psychopy.org/t/typeerror-cannot-read-property-concat-of-undefined/22734 "2021-05-03T10:15:13Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![milsandhills](https://avatars.discourse-cdn.com/v4/letter/m/50afbb/32.png) [@milsandhills](https://discourse.psychopy.org/u/milsandhills)
#### Post date: [May 3, 2021, 10:15am UTC](https://discourse.psychopy.org/t/typeerror-cannot-read-property-concat-of-undefined/22734/1 "2021-05-03T10:15:14Z")

</div>

**URL of experiment** : [https://gitlab.pavlovia.org/CWoodrow/student\_computer\_experiment\_v1](https://gitlab.pavlovia.org/CWoodrow/student_computer_experiment_v1)

**Description of the problem** : When I run my experiment I get an error which says, ‘TypeError: Cannot read property ‘concat’ of undefined’. The error is in a routine which has two code components - one to allow participants to see what they type on-screen, and another to get the key durations for the keys they press. This works in PsychoPy but not in Pavlovia. The first routine that runs as intended contains the same code that allows participants to see what they type, so my assumption is that the error must be in relation to the key duration code, but I can’t figure out what.

Here is what is in my keyDuration code component:

**Begin routine**

```auto

mykb = new core.Keyboard({psychoJS: psychoJS, clock: new util.Clock(), waitForStart: true});
mykb.start();
keysWatched = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p",
"q","r","s","t","u","v","w","x","y","z","backspace","enter","space",",",
".","/", "backslash", "'",";","[","]","1","2","3","4","5","6","7","8","9","0",
"shift", "capslock", "tab", "control", "alt", "`", "-", "=", "meta", "contextmenu",
"delete", "pound", "arrowleft", "arrowright", "arrowup", "arrowdown", "insert",
"printscreen", "home", "end", "pagedown", "pageup", "pause"];
status = ["up", "up", "up", "up", "up", 
"up","up","up","up","up","up","up","up","up","up","up","up","up","up","up","up",
"up","up","up","up","up","up","up","up","up","up","up","up","up","up","up","up",
"up","up","up","up","up","up","up","up","up","up","up","up","up","up","up","up",
"up","up","up","up","up","up","up","up","up","up","up","up","up","up","up","up",
"up"];
keyCount = 0;
currentKey = 0;
statusList = [];
pressTime = 0;
keyPressTime = [];
liftTime = 0;
keyDur=[];

```

**Each frame**

```auto
// Loop through keyboard events to update keyStates
let keyEvents = mykb.getEvents();
// Get keys?
let keys = mykb.getKeys({keyList: keysWatched, waitRelease: false, clear: false});
key_list = key_list.concat(keys);
// Previous status of key we are currently watching
let previousStatus = status[currentKey];
// Key, timestamp, and keyDown, of current key event
let keyIndex, key, keyStatus;
for (let i = 0; i < keyEvents.length; i++) {
    // Current key event
    key = keyEvents[i].pigletKey;
    keyStatus = keyEvents[i].status === Symbol.for('KEY_DOWN')? 'down': 'up';
    // Index of this key in keysWatched. NB findIndex not supported by IE
    keyIndex = keysWatched.indexOf(key);
    // Key is one that we watch; update its status
    if (keyIndex !== -1) {
        status[keyIndex] = keyStatus
        statusList.append(keyStatus)
        console.log('keyStatus')
        console.log(statusList)
    }
}
if ((statusList.length > 1)) {
    if ((statusList.slice((- 1))[0] !== statusList.slice((- 2))[0])) {
        if ((statusList.slice((- 1))[0] === "down")) {
            pressTime = taskClock.getTime() * 1000
            keyPressTime.append(pressTime)
            taskClock.reset()
        } else {
            if ((statusList.slice((- 1))[0] === "up")) {
                liftTime = taskClock.getTime() * 1000
                keyDur.append(liftTime)
                taskClock.reset()
            }
        }
    }
} else {
    if ((statusList.length == 1)) {
        pressTime = taskClock.getTime() * 1000
        keyPressTime.append(pressTime)
        taskClock.reset()
    }
}

```

I have tried amending the code in various ways, such as using `event.` instead of `mykb.`. I have shortened the `keysWatched` list to just two keys to see if the issue is related to the key names. I have also tried using `.append` instead of `.concat`. None of these have helped or changed the error at all.

Any help hugely appreciated! The full JS script can be found in the gitlab link above in the file called ‘StudentExperiment\_wordsX2\_v6.js’

---

<div class="post-metadata">

### Author: ![jonathan.kominsky](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@jonathan.kominsky](https://discourse.psychopy.org/u/jonathan.kominsky)
#### Post date: [May 3, 2021, 2:22pm UTC](https://discourse.psychopy.org/t/typeerror-cannot-read-property-concat-of-undefined/22734/2 "2021-05-03T14:22:40Z")

</div>

I think this is your culprit:

```auto
key_list = key_list.concat(keys);

```

You’re calling ‘concat’ on key\_list during the initial definition of key\_list. I see why you want to do it that way, but you need to initialize it or the first time it’s quite literally trying to add onto something that doesn’t exist yet. I think if you initialize key\_list with everything else in your begin routine code it should just work:

```auto
mykb = new core.Keyboard({psychoJS: psychoJS, clock: new util.Clock(), waitForStart: true});
mykb.start();
keysWatched = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p",
"q","r","s","t","u","v","w","x","y","z","backspace","enter","space",",",
".","/", "backslash", "'",";","[","]","1","2","3","4","5","6","7","8","9","0",
"shift", "capslock", "tab", "control", "alt", "`", "-", "=", "meta", "contextmenu",
"delete", "pound", "arrowleft", "arrowright", "arrowup", "arrowdown", "insert",
"printscreen", "home", "end", "pagedown", "pageup", "pause"];
status = ["up", "up", "up", "up", "up", 
"up","up","up","up","up","up","up","up","up","up","up","up","up","up","up","up",
"up","up","up","up","up","up","up","up","up","up","up","up","up","up","up","up",
"up","up","up","up","up","up","up","up","up","up","up","up","up","up","up","up",
"up","up","up","up","up","up","up","up","up","up","up","up","up","up","up","up",
"up"];
keyCount = 0;
currentKey = 0;
statusList = [];
pressTime = 0;
keyPressTime = [];
liftTime = 0;
keyDur=[];
key_list=[]; //This line should be all you need.

```

---

<div class="post-metadata">

### Author: ![milsandhills](https://avatars.discourse-cdn.com/v4/letter/m/50afbb/32.png) [@milsandhills](https://discourse.psychopy.org/u/milsandhills)
#### Post date: [May 3, 2021, 2:40pm UTC](https://discourse.psychopy.org/t/typeerror-cannot-read-property-concat-of-undefined/22734/3 "2021-05-03T14:40:26Z")

</div>

Hi @jonathan.kominsky, thanks so much for the quick response! I tried adding this to my code and am getting exactly the same error… any other ideas?

Many thanks!

---

<div class="post-metadata">

### Author: ![jonathan.kominsky](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@jonathan.kominsky](https://discourse.psychopy.org/u/jonathan.kominsky)
#### Post date: [May 3, 2021, 3:07pm UTC](https://discourse.psychopy.org/t/typeerror-cannot-read-property-concat-of-undefined/22734/4 "2021-05-03T15:07:17Z")

</div>

Hmm. The basic issue is still likely that for whatever reason when “concat” is called, JS does not recognize that key\_list has been initialized, but now I’m not sure why that is.

Can you have the log output key\_list right before the concat line? It should show you an empty array. Also, if you can get a line number on the error message from the log, that would be helpful in making sure that particular instance of concat is the culprit.

---

<div class="post-metadata">

### Author: ![milsandhills](https://avatars.discourse-cdn.com/v4/letter/m/50afbb/32.png) [@milsandhills](https://discourse.psychopy.org/u/milsandhills)
#### Post date: [May 4, 2021, 8:49am UTC](https://discourse.psychopy.org/t/typeerror-cannot-read-property-concat-of-undefined/22734/5 "2021-05-04T08:49:19Z")

</div>

@jonathan.kominsky the plot thickens! After doing some more digging and checking the log it seemed the error was actually related to the term ‘keys’. I realised that I was calling `keys` in a different code component in the same routine, so to see if this clash was an issue, I changed `keys` to `keys1` in the keyDuration code.

Once I did this the error changed to ‘ReferenceError: Cannot access ‘keys’ before initialization’, and again, the error seemed to be related to my different code component. This made absolutely NO sense to me, because as I mentioned previously, that code component is identical to one in a different routine that works perfectly.

I then decided to try the experiment in a different browser (Edge) and it WORKS exactly as intended! So the issue seems to be related to Chrome, though I have no idea what. This is concerning as ideally I’d want participants to be able to access the experiment from various browsers.

For reference, here is the code in my second component that seemed to be causing an issue in Chrome. The error related to the line, `keys = event.getKeys();`:

**Begin routine**

```auto
modify = false;
text.text = " ";
event.clearEvents();

```

**Each frame**

```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);
keys = event.getKeys();
if (keys.length) {
    if (_pj.in_es6("space", keys)) {
        text.text = (text.text + " ");
    } else {
        if (_pj.in_es6("backspace", keys)) {
            text.text = text.text.slice(0, (- 1));
        } else {
            if ((_pj.in_es6("lshift", keys) || _pj.in_es6("rshift", keys))) {
                modify = true;
            } else {
                if (_pj.in_es6("return", keys)) {
                    continueRoutine = false;
                } else {
                    if (modify) {
                        text.text = (text.text + keys[0].upper());
                        modify = false;
                    } else {
                        text.text = (text.text + keys[0]);
                    }
                }
            }
        }
    }
}

```

Any ideas as to why Chrome could dislike this, much appreciated!

---

<div class="post-metadata">

### Author: ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)
#### Post date: [May 4, 2021, 8:54am UTC](https://discourse.psychopy.org/t/typeerror-cannot-read-property-concat-of-undefined/22734/6 "2021-05-04T08:54:53Z")

</div>

Have you defined event as per my crib sheet or code snippets pages? If not then keys = event.getKeys(); will fail online and keys will be undefined.

> [@Well, JS code errors on Pavlovia](https://discourse.psychopy.org/t/well-js-code-errors-on-pavlovia/22570/30):
>
> You’ve linked to my code snippets page rather than my crib sheet. However, at the top it says: Code components should be Auto Py-\>JS unless code has been added to the PsychoJS column. I would recommend that all experiments intended to run online contain a JavaScript code component in their first routine called code\_JS with the following in Begin Experiment. thisExp=psychoJS.experiment; win=psychoJS.window; event=psychoJS.eventManager; shuffle = util.shuffle; Array.prototype.append = [].push;…

---

<div class="post-metadata">

### Author: ![milsandhills](https://avatars.discourse-cdn.com/v4/letter/m/50afbb/32.png) [@milsandhills](https://discourse.psychopy.org/u/milsandhills)
#### Post date: [May 4, 2021, 8:57am UTC](https://discourse.psychopy.org/t/typeerror-cannot-read-property-concat-of-undefined/22734/7 "2021-05-04T08:57:02Z")

</div>

Hi @wakecarter, thanks for the response. Yes, I have. My initial JS code component has the following:

**Begin experiment**

```auto
thisExp=psychoJS.experiment;
win=psychoJS.window;
event=psychoJS.eventManager;
shuffle = util.shuffle;
Array.prototype.append = [].push;

```

Also, as I mentioned, I have used ` keys = event.getKeys();` in a different earlier routine and this works fine.

---

<div class="post-metadata">

### Author: ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)
#### Post date: [May 4, 2021, 9:09am UTC](https://discourse.psychopy.org/t/typeerror-cannot-read-property-concat-of-undefined/22734/8 "2021-05-04T09:09:15Z")

</div>

Why are there so many psyexp files in your [repository](https://gitlab.pavlovia.org/CWoodrow/student_computer_experiment_v1)? It might be worth trying from a fresh folder.

I use Chrome and haven’t come across issues with keys = event.getKeys() with 2020.2.10 so I think I’d need to see the error live. Are you able to set the experiment to running or share it with me so I can pilot it?

---

<div class="post-metadata">

### Author: ![milsandhills](https://avatars.discourse-cdn.com/v4/letter/m/50afbb/32.png) [@milsandhills](https://discourse.psychopy.org/u/milsandhills)
#### Post date: [May 4, 2021, 9:32am UTC](https://discourse.psychopy.org/t/typeerror-cannot-read-property-concat-of-undefined/22734/9 "2021-05-04T09:32:50Z")

</div>

Hi @wakecarter, apologies for the confusion, I had all my various experiment versions saved in one folder until things were finalised and working. I am trying to create a new repository but having sync issues so I will keep working on this.

I have added you as a member to the current (messy) repository with guest access, so let me know if you are able to run the experiment and whether you experience the same issue. The correct file is **StudentExperiment\_wordsX2\_v6.psyexp**

Your help is much appreciated!

---

<div class="post-metadata">

### Author: ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)
#### Post date: [May 4, 2021, 9:38am UTC](https://discourse.psychopy.org/t/typeerror-cannot-read-property-concat-of-undefined/22734/10 "2021-05-04T09:38:17Z")

</div>

I tried piloting it and didn’t get any errors (though it didn’t run in full screen and being asked to type before that was possible was disconcerting).

[PARTICIPANT\_StudentExperiment\_wordsX2\_v6\_2021-05-04\_10h34.22.135.csv](https://discourse.psychopy.org/uploads/short-url/3UIFuqgxeU4tUzM24M5iy3bgdDF.csv) (9.2 KB)

---

<div class="post-metadata">

### Author: ![milsandhills](https://avatars.discourse-cdn.com/v4/letter/m/50afbb/32.png) [@milsandhills](https://discourse.psychopy.org/u/milsandhills)
#### Post date: [May 4, 2021, 9:45am UTC](https://discourse.psychopy.org/t/typeerror-cannot-read-property-concat-of-undefined/22734/11 "2021-05-04T09:45:35Z")

</div>

@wakecarter Thanks for letting me know it wasn’t in full screen. I think I must haver monitor settings currently set to fit my laptop, and didn’t know this would still apply in Pavlovia.

I’m still getting the error in chrome, as below:

 ![image](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/1/d/1d1d961e6a472003340a45d2759f3bbe7fe26ff8.png)

This appears when I go to type the word ‘linear’ for the second time. It is still flagging the error at `keys = event.getKeys();`

Not sure why it would be fine for you and not me?

---

<div class="post-metadata">

### Author: ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)
#### Post date: [May 4, 2021, 9:48am UTC](https://discourse.psychopy.org/t/typeerror-cannot-read-property-concat-of-undefined/22734/12 "2021-05-04T09:48:32Z")

</div>

Have you tried to flush the cache using Ctrl-Shift-R ?

Did you previously have keys in a Before Experiment tab?

---

<div class="post-metadata">

### Author: ![milsandhills](https://avatars.discourse-cdn.com/v4/letter/m/50afbb/32.png) [@milsandhills](https://discourse.psychopy.org/u/milsandhills)
#### Post date: [May 4, 2021, 9:51am UTC](https://discourse.psychopy.org/t/typeerror-cannot-read-property-concat-of-undefined/22734/13 "2021-05-04T09:51:29Z")

</div>

No, I haven’t tried that. Would you be able to explain how I do this, or direct me to somewhere with instructions?

And no, I’ve never had keys in the Before Experiment tab.

---

<div class="post-metadata">

### Author: ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)
#### Post date: [May 4, 2021, 9:52am UTC](https://discourse.psychopy.org/t/typeerror-cannot-read-property-concat-of-undefined/22734/14 "2021-05-04T09:52:17Z")

</div>

I’ve just seen that you are printing key status every frame. That looks like a significant computational overhead.

---

<div class="post-metadata">

### Author: ![milsandhills](https://avatars.discourse-cdn.com/v4/letter/m/50afbb/32.png) [@milsandhills](https://discourse.psychopy.org/u/milsandhills)
#### Post date: [May 4, 2021, 9:55am UTC](https://discourse.psychopy.org/t/typeerror-cannot-read-property-concat-of-undefined/22734/15 "2021-05-04T09:55:47Z")

</div>

@wakecarter it works! I used Crtl+Shift+R on the experiment tab and when the page reloaded it worked! I cannot thank you enough, I never would’ve thought to try something so simple.

Re. printing key status, this is necessary to determine which keys are down and up and hence compute a press and lift time for the keys. Though if you have a better way of coding this, do let me know! This code was borrowed from one of Becca and Thomas Pronk’s experiments, as I’m a newbie myself.
