Cannot read property '0' of undefined

URL of experiment: https://pavlovia.org/run/juliankl/finalorteng9/html/

Gitlab: Julian Kirkeby Lysvik / finalorteng9 · GitLab

Experiment created using builder on Windows, version 3.0.4.

When running my experiment I get the following error:

error_pavlovia5

Looking online I suspect the error could be related to a code component in my builder which allows me to register and display participant input, live. The Python code is the following (from: GitHub - jacanterbury/PsychoPy-snippets: Some PsychoPy example scripts & snippets for easy sharing)

#Begin experiment
inputText = ""

#Begin routine
theseKeys=""
shift_flag = False
txtinp.alignHoriz ='center'

#Each frame
n= len(theseKeys)
i = 0
while i < n:

    if theseKeys[i] == 'return' and len(inputText) >1:
        # pressing RETURN means time to stop
        continueRoutine = False
        break

    elif theseKeys[i] == 'backspace':
        inputText = inputText[:-1]  # lose the final character
        i = i + 1

    elif theseKeys[i] == 'space':
        inputText += ' '
        i = i + 1

    elif theseKeys[i] in ['lshift', 'rshift']:
        shift_flag = True
        i = i + 1

    else:
        if len(theseKeys[i]) == 1:
            # we only have 1 char so should be a normal key, 
            # otherwise it might be 'ctrl' or similar so ignore it
            if shift_flag:
                inputText += chr( ord(theseKeys[i]) - ord(' '))
                shift_flag = False
            else:
                inputText += theseKeys[i]

        i = i + 1


#End routine
# let's store the final text string into the results file...
thisExp.addData('inputText', inputText)
inputText=""

The custom code is part of this full structure, where “textinp” displays “$inputText” from the code above.

My js-“translation” is a simple code:

#Begin experiment
function inputText() {
  var x = document.createElement("INPUT");
  x.setAttribute("type", "text");
  x.setAttribute("value", "");
  document.body.appendChild(x);
}

So I suspect the problem is my js-code. However, as far as I know it could be something else entirely. Any help would be greatly appreciated

Hi @Nessimon, I think one problem you will have is using a numpy method to select your rows. Numpy is a Python library, so you will have to find an alternative method for choosing your rows for the JavaScript experiment.

1 Like

Thanks a lot! That worked, now I can get to the first part of my experiment:
https://pavlovia.org/run/juliankl/finorteng2/html/

But I believe the above is still a problem, I now get the error message:
error_pavlovia6

Thanks again.

Great. Looks like your function inputText is not available globally, so when you use inputText, it appears as not defined. To get PsychoPy to do this for you, just create an inputText variable before you create the function e.g., inputText = '';. That way, inputText should be defined globally outside of the experimentInit function.

1 Like

Thank you! Now the error message has disappeared, but it still does not register my keyboard input and display it, like my python code does. Any suggestions?

Experiment: https://pavlovia.org/run/juliankl/finorteng3/html/
Code: https://gitlab.pavlovia.org/juliankl/finorteng3

I think you need to work on your JS code and make use of the PsychoJS text objects, as you would with Python, rather than attempting to manipulate text using HTML commands.

1 Like

Here is an example of how you would get typed text to appear on screen online. Assuming you have a text component called “text”, and a code component - in “Every Frame”:

let theseKeys = psychoJS.eventManager.getKeys();
if (theseKeys.length > 0) {  // at least one key was pressed
  textAdd = theseKeys[theseKeys.length-1]; 
  }

// check for quit:
if (theseKeys.indexOf('escape') > -1) {
    psychoJS.experiment.experimentEnded = true;
}
if (textAdd) {
    text.setText(text.text + textAdd);  
    textAdd = undefined;
}

@Nessimon I have a similar problem; what alternative did you use for the numpy method in javascript?