psychopy.org | Reference | Downloads | Github

TypeError: Cannot read property '0' of undefined

Code:https://gitlab.pavlovia.org/weimin023/exp5_0613

Image below is the flow of my design. I guess the error is in the “red circle” in the image.
Imgur

And the code of this part is:

**Begin Experiment:**
Bigpool2image=' '

**Begin Routine:**
if BigpoolAns.keys[0]=='1':
    Bigpool2image='003.jpeg'
if BigpoolAns.keys[0]=='2':
    Bigpool2image='004.jpeg'
if BigpoolAns.keys[0]=='3':
    Bigpool2image='005.jpeg'
if BigpoolAns.keys[0]=='4':
    Bigpool2image='006.jpeg'
if BigpoolAns.keys[0]=='5':
    Bigpool2image='007.jpeg'
if BigpoolAns.keys[0]=='6':
    Bigpool2image='008.jpeg'

Can someone tell me where is the problem?
Thanks a lot!

Hi @Weimin_Chan, I think the error is the way you have defined your variables in your code component. e.g.,

var odd = [1,2];
var smallpass=2
var bigpass=2
var group = odd[Math.floor(Math.random() *odd.length)];
  if (group==1){

Whilst this is the correct way to define variables in JS( i.e., using the var prefix) you do not need to define your variables in your code component because Builder will do this for you in such a way that will give your variables global scope in your task. By redefining your variables using var, you are limiting their scope to the function that PsychoPy puts them in (see your JS scripts). I think the error is because your variable bigpass needs global scope to be used to define nReps in your trialhandler.

Hi @dvbridges,
So maybe I should put var bigpass=2 out of the function experimentInit()?

You could, but it would be easier if you just delete the var prefix from your variables in your code component and let PsychoPy do it for you.

@dvbridges
Thanks for your kindly reply.
I just wonder if I let PsychoPy declare the variable for me, how can I initialize the variable to certain value?
I have started to learn JS…

Its ok, if you assign a value to a variable in a code component e.g. bigpass=2;, PsychoPy will declare that variable for you, and give it global scope e.g. var bigpass;.

@dvbridges
OMG, it works! Thank you!!!
It torments me for days.

But there’s another error occurs.
Imgur
It’s seems like this is an error about image path?