psychopy.org | Reference | Downloads | Github

Error translating .append function to work on Pavlovia

What are you trying to achieve?:

I created a task on Builder and on each trial I add the participant’s response time to a running list. To create this list I am using the .append function.

What did you try to make it work?:

I created a JS code component where in the *Begin Experiment tab I pasted in this code:

Array.prototype.append = [].push;

as has been recommended on other threads and the google crib sheet.

What specifically went wrong when you tried that?:

The task works fine when it’s run from the Builder but when I run the task on Pavlovia I receive the following error:
TypeError: Cannot read property ‘append’ of undefined

I am uncertain if this error is indicating that I maybe put the java code in the wrong place and it still can’t translate the .append function or if the code I wrote using .append is the source of the error?

In case it is helpful, below is the python code I am using to collect response times with the .append function that’s based on another thread.

**Begin Routine***
if trials.thisN == 0:
rt_aggregate = []

**End Routine***
# the code below tells the task only to collect RT from correct trials when participants were supposed to press a button rather than if they had accidentally hit the button when they heard a signal not to.
if ssig:
    rt_aggregate.append(0)
elif Letter_resp.rt and Letter _resp.corr == 1:
    rt_aggregate.append(Letter _resp.rt)
else:
    rt_aggregate.append(0)
thisExp.addData('rt_aggregate', rt_aggregate) #save to output data file

Thank you for your help!

Hi There,

I think that this is an issue with using dots in the variable name in your first code component (dots indicate you are accessing a property of an existing variable, and that variable doesn’t yet have the property ‘append’ which gives the error).

To implement push in JS you should just be able to change your code type to ‘both’ and replace the .append parts with .push on the JS side - what happens if you do that?

Thanks,
Becca

Hi Becca - Thank you so much for your help and apologies for the long delay in my response!

I replaced .append with .push on the JS side, which definitely moved me in the right direction. I now get the error:

TypeError: Cannot read property ‘push’ of undefined

This makes me think that there is something wrong with how I have coded my “rt_aggregate = [ ]” array in JS, specifically, that is causing the error and preventing .push from recognizing it.

+1 to this, I have sometimes had issues with this as well. Where possible I’ve just changed the append into a list comprehension (e.g. newList = [i for i in otherList]), but of course that can’t cover all the cases. It would be nice to be able to use .append again!

Hi Both,

you should be able to use ‘push’ in JS the same way that you use ‘append’ locally in python (quick demo here https://gitlab.pavlovia.org/lpxrh6/listdemo)

The error:
TypeError: Cannot read property ‘push’ of undefined
seems to refer to the variable that you are trying to append is undefined. To check that there is a list to add to try printing it to console:

console.log(x)

where x is the name of the thing you are trying to append/push. when you open developer tools (ctl > shit >I on windows) you should see ‘Array’ printed out. If you so not, the error is not from your push function, it is because the list you are trying to add to isn’t defined.

Let us know how you get on,
Becca