TypeError: Cannot read property 'undefined' of undefined

Description of the problem: hello. I designed a dual task in builder mode and now trying to move it to the online platform via pavlovia. when I run the experiment, I end up with the error in the title “TypeError: Cannot read property ‘undefined’ of undefined” in the following line:

prac_selectedDigit = prac_cond[j];

where prac_selectedDigit, prac_cond, and j are all defined with “var” in above lines in the code.

prac_cond is defined as:
prac_cond = [5, 6, 7, 5, 6, 7];

the full part of the code where I encounter the error is as follows:

function practiceNumberTaskRoutineBegin(trials) {
  return function () {
    //------Prepare to start Routine 'practiceNumberTask'-------
    t = 0;
    practiceNumberTaskClock.reset(); // clock
    frameN = -1;
    routineTimer.add(1.800000);
    // update component parameters for each repeat
    
            // add-on: list(s: string): string[]
            function list(s) {
                // if s is a string, we return a list of its characters
                if (typeof s === 'string')
                    return s.split('');
                else
                    // otherwise we return s:
                    return s;
            }
            
            prac_selectedDigit = prac_cond[j]; //THIS IS THE ERROR LINE
    prac_leftRightCond = prac_leftRightFull[j];
    shuffle(prac_fullList);
    if ((prac_selectedDigit === 5)) {
        prac_fiveDigs = prac_fullList.slice(0, 5);
        prac_unique = list((set(prac_fullList) ^ set(prac_fiveDigs)));
        prac_toPrint = Number.parseInt("".join(map(str, prac_fiveDigs)));
        shuffle(prac_fiveDigs);
        shuffle(prac_unique);
        if ((prac_leftRightCond === 0)) {
            prac_digit1 = prac_unique[0];
            prac_digit2 = prac_fiveDigs[0];
        } else {
            if ((prac_leftRightCond === 1)) {
                prac_digit1 = prac_fiveDigs[0];
                prac_digit2 = prac_unique[0];
            }
        }
    } else {
        if ((prac_selectedDigit === 6)) {
            prac_sixDigs = prac_fullList.slice(0, 6);
            prac_unique = list((set(prac_fullList) ^ set(prac_sixDigs)));
            prac_toPrint = Number.parseInt("".join(map(str, prac_sixDigs)));
            shuffle(prac_sixDigs);
            shuffle(prac_unique);
            if ((prac_leftRightCond === 0)) {
                prac_digit1 = prac_unique[0];
                prac_digit2 = prac_sixDigs[0];
            } else {
                if ((prac_leftRightCond === 1)) {
                    prac_digit1 = prac_sixDigs[0];
                    prac_digit2 = prac_unique[0];
                }
            }
        } else {
            if ((prac_selectedDigit === 7)) {
                prac_sevenDigs = prac_fullList.slice(0, 7);
                prac_unique = list((set(prac_fullList) ^ set(prac_sevenDigs)));
                prac_toPrint = Number.parseInt("".join(map(str, prac_sevenDigs)));
                shuffle(prac_sevenDigs);
                shuffle(prac_unique);
                if ((prac_leftRightCond === 0)) {
                    prac_digit1 = prac_unique[0];
                    prac_digit2 = prac_sevenDigs[0];
                } else {
                    if ((prac_leftRightCond === 1)) {
                        prac_digit1 = prac_sevenDigs[0];
                        prac_digit2 = prac_unique[0];
                    }
                }
            }
        }
    }
    j += 1;
    
    prac_numberStim.setText(prac_toPrint);
    // keep track of which components have finished
    practiceNumberTaskComponents = [];
    practiceNumberTaskComponents.push(prac_numberStim);
    
    for (const thisComponent of practiceNumberTaskComponents)
      if ('status' in thisComponent)
        thisComponent.status = PsychoJS.Status.NOT_STARTED;
    
    return Scheduler.Event.NEXT;
  };
}

in Builder,this part is in the “begin routine” tab in the code component.

I cant see why I get the “undefined” error although I defined all the variables. Can anybody help me?

thanks in advance, tutku

Hi There,

As a first step can I recommend adding the following statements at the point where you think your variables are being defined:

console.log(prac_selectedDigit )
console.log(prac_cond)
console.log(j)

This will help in double checking that your variables are being defined (or which one we are having trouble with!) before moving on.

Thanks,
Becca

Hi Becca, thank you for your reply.

I define all my variables at the very beginning of my js code. Thus I added the part of code that you recommended me right below the line where I define my variables with “var”:

`var prac_cond;
var j;
var i;
var prac_leftRightCond;
var prac_selectedDigit;
var prac_leftRightFull;
var prac_fullList;

console.log(prac_selectedDigit )
console.log(prac_cond)
console.log(j)`

nothing has changed though, the undefined error persists.

:frowning:

edit: j is both defined with “var” and j = 0; btw

what printout do you get in your console when you run online though? does this tell you which of these variables JS thinks is undefined or do the expected variable values print out in the console when you run the task online? (you can see the crib sheet for how to open developer tools https://docs.google.com/document/d/13jp0QAqQeFlYSjeZS0fDInvgaDzBXjGQNe4VNKbbNHQ/edit)

Becca

Try not doing that. Explicitly using var can restrict the scope to a function that runs at the start of the script, meaning that it isn’t available when you need it later.

ok, where in the code should I do that instead? any suggestions?

you could try removing var then open up your console when running online and let us know what is printed out (this will give us clearer indication which variable is ‘undefined’ so that we can work on debugging it.

Let us know how you get on :slight_smile:
Becca