psychopy.org | Reference | Downloads | Github

TypeError cannot read property '0' of undefined in Pavlovia

Hello,

I am working on an experiment using the Builder in PsychoPy and uploading it to Pavlovia. In my experiment, I am shuffling videos and feedback. The Feedback_list is:

var Feedback_list = [“Correct”, “Incorrect”, “Corr_inc”, “Praise”, “Reminder”, “Praise_and_reminder”]

At the beginning of my experiment, I set Video_num = -1, and then, when I enter a loop, I incrementally add 1 to Video_num like so:

Video_num = (Video_num + 1);

Since JavaScript is 0 based, when I index into my Feedback_list the first time, Video_num = 0 (it should pull the first video in the shuffled Feedback_list).

My experiment uploads to Pavlovia with no error. However, when I run it, I am getting an error on this line:

Feedback = Feedback_list[Video_num];

with this error message:

TypeError: Cannot read property ‘0’ of undefined.

I am not sure why I am getting this error or why it is not pulling from the first instance in the shuffled Feedback_list.

Thank you in advance.

I suspect you have one or both of the following issues.

  1. If you use var when defining a variable its scope is limited to that component.

  2. You may not have defined the shuffle function. Look at my crib sheet (pinned post) for my code_JS recommendations

Removing var worked. Thank you!

I am hoping you can help me with one more issue I am having.

My participants are expected to press specified keys when certain behaviors (BOI) occur within videos. I have set up ranges of time for each of the responses, and written code to check if the key was pressed within that range. There are no more than 8 possible correct responses for each video. This is the code I have under each frame to record the key presses:

if (psychoJS.eventManager.getKeys(BOI)) {
t = trialClock.getTime();
correctRange.some(x => (x[0] < t) & (x[1] > t));
if (correctRange) {
correct +=1;
} else {
incorrect +=1;
}
}

When I run the experiment myself, and I press nothing, I get feedback that says I got 2,349 correct responses out of the possible 4. My feedback code is as follows:

if (Feedback === “Corr_inc”){
msg = (You got ${correct} out of ${Poss_corr} instances of ${Behavior} correct.) ;

Poss_corr is defined as total_incorrect = (Poss_corr - correct);

When I run the experiment, the Poss_corr portion is calculating correctly (there are 4 possible correct responses for the video I am referencing). However, I am not sure why it is coming back with 2,349 correct responses when a) I didn’t press any keys and b) there are only 4 possible correct answers.

Thank you.

When you press a key it will be down for multiple frames and I think that you code will add a point for each frame.

I don’t think you should use t which is already used as time since the start of the routine.

Does BOI change as the routine progresses?

Should it be &&?

Best wishes,

Wakefield

Hi Wakefield,

Thanks so much for the quick response. I think && is a great suggestion, and I made a change to t and used a different letter.

Yes, BOI does change per loop. I have 6 videos. The first video plays 3 times within the loop, feedback is giving, and then the participant goes through the loop again, receiving new instructions, and a new video is selected and shown 3 times. So it’s a new BOI for each video that plays.

I am new to JavaScript…how would you suggest I can begin fixing the key pressing issue? I ran the experiment again and did not press any keys at all except the space bar to move to the next page of instructions. The feedback I received at the end of the routine was that I got -2,644 instances of the BOI incorrect. None of the keys that I have identified as “correct keys” were pressed.

Thanks in advance.

I’m afraid that my JavaScript isn’t up to knowing whether correctRange.some(x => (x[0] < t) & (x[1] > t)); is valid syntax (especially .some – what does that mean?), but I suspect that it’s evaluating as true. You should try printing (console logging) x[0], x[1] and t.

That syntax is correct. The .some checks to see if any element of the array fits in the function (so in my case, if any number in my given array fits within the ranges).

I moved it to end routine instead of each frame based on what you said about it adding 1 in each of the frames. I am heading in the right direction. I am not getting large numbers anymore, so I’ll keep fine tuning. Thank you for all of your help.

Hello,
I am getting the same error on Pavlovia. In my experiment I show videos and I want my participants to type what they perceive. Currently I am suing the latest version of PsychoPy that includes “editable textbox.”

Any help would be appreciated.

Here is a link: https://pavlovia.org/SerenaZa/final-sr-or-list-1_new-version

Hi all,

I am posting my question here since I encounter this error when running my task online.

To provide a bit of context, I am running a matching pennies game, where participants have to guess the choice of a virtual agent - it’s a binary choice, between a blue or red card -. I have already created the task using the builder with several agents with increasing levels of bias in their decision (e.g., bias50, bias60, bias70…)

This task is already available online here: Online_ToM [PsychoPy]

Now I am trying to randomize the order of the agents (e,g., bias70, bias50, bias60…), I followed the guidance from this post, adding an outloop with an excel file with the name of the condition as columns + rows with 0 and 1 for each condition: Randomization of several loops - #12 by wakecarter

It’s working perfectly fine in local, however when pushed on Pavlovia I got the “TypeError cannot read property ‘0’ of undefined in Pavlovia” when starting the randomization outloop.

Here is the URL of the experiment: Online_ToM [PsychoPy]

My guess is that the JS script doesn’t like the randomization file with 0 and 1 but I am not familiar with this language so not sure if my error is related to that or not.

If someone has any clue about what’s going on, I will be interested to hear about your feedback/advice.

Thank you very much for your help.

Julia

The error appears to be in the line

for (const thisTrial50 of trial50) {

That sounds like you might have a blank or undefined nReps in your loop. Alternatively you are trying to select a single row using a method that doesn’t work online.

Please could you show your loop definition.

Hi @wakecarter,

Thank you for your quick reply.

Here is the definition for trial50:
Capture d’écran 2021-08-03 à 15.30.01

Here is the definition for the outloop:
Capture d’écran 2021-08-03 à 15.30.34

Here is the screen of the builder:

I have 1 loop (e.g., TOM_50) for each condition that provide agent resp, see below caption:
Capture d’écran 2021-08-03 à 15.33.11

Let me know if you need anything else.

Thank you for your help

Julia

You are using trial50 as the name of a loop and a value for nReps

Ah yes indeed, problem solved :+1:

I changed the name of the nReps and conditions in the excel file and it’s working online and offline.

Thank you very much for your help.