Cannot read property 'length' of undefined (getting typed response on Pavlovia)

URL of experiment: Joy Na / Inverted text recall · GitLab

Description of the problem:

Hello.
I am trying to get typed response in Korean, so I used custom code as below:


This runs fine on local computer, but I run into property ‘length’ problem on pavlovia.

var resplist;
var addKey;
function recallRoutineEachFrame(trials) {
  return function () {
    //------Loop for each frame of Routine 'recall'-------
    let continueRoutine = true; // until we're told otherwise
    // get current time
    t = recallClock.getTime();
    frameN = frameN + 1;// number of completed frames (so 0 is the first frame)
    // update/draw components on each frame
    resplist = psychoJS.eventManager.getKeys();
    if (resplist) {
        addKey = resplist[0];
        if (addKey.length === 1) {
            response += addKey;
        } else if (addKey === "space") {
            response += " ";
        } else if (addKey === "backspace" && response.length > 0) {
            response = response.slice(0, - 1);
        } else if (addKey === "return") {
            continueRoutine = false;
        }
    }
    
    

Above is from the javascript file, and it seems that addKey.length === 1 is what’s causing the problem.
How can I get this to work?

Thanks in advance!

I think your issue is if resplist:.

Try replacing it with if len(resplist):

What a simple, but precise solution! It worked marvelously, thank you.
However, pavlovia is not responding to switch language key. Is there a way around this?

Hi, I am also experiencing a similar problem, I get * TypeError: Cannot read property ‘length’ of undefined when testing on pavlovia. I am working on a digit span here is the link ForwardsSpan [PsychoPy] and the coding. Any help is much appreciated

This is what the JS looks like leading up to that error:

  if (key_resp_2.status === PsychoJS.Status.STARTED) {
      let theseKeys = key_resp_2.getKeys({keyList: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'return', 'backspace'], waitRelease: false});
      _key_resp_2_allKeys = _key_resp_2_allKeys.concat(theseKeys);
      if (_key_resp_2_allKeys.length > 0) {
        key_resp_2.keys = _key_resp_2_allKeys.map((key) => key.name);  // storing all keys
        key_resp_2.rt = _key_resp_2_allKeys.map((key) => key.rt);
      }
    }
    
    if ((key_resp_2.keys.length > 0)) {

So – key_resp_2.keys is only defined if:

key_resp_2.status === PsychoJS.Status.STARTED
and
if (_key_resp_2_allKeys.length > 0)

So you could try using
if (_key_resp_2_allKeys.length > 0)

1 Like