TypeError: .shift() is not a function + .pop() is not a function

Hello all,

I am trying to upload online an experiment with presentation of images. To present the images I used the function > imagelist.pop() offline with Auto->JS option. It works beautifully in my laptop but when I try to upload it online something very weird happens. During the training session the pop function works all well online, but when it gets to the task there is the error .pop() is not a function. I tried to change the function with

.shift() based on the suggestions here : PsychoPy Python to Javascript crib sheet - Google Docs.
However, now I get the error .shift is not a function. Any clue or someone with similar problem?

I tried with having the code to BOTH so in python .pop() and JS .shift() but still the same error.
Thanks in advance and my best wishes
Anastasia

Hi Anastasia,

What is the output in the console of:

console.log('imagelist = ', imagelist);
console.log('type of imagelist = ', typeof imagelist);

Place them just before imagelist.pop()

Yiannis

1 Like

Hello Yiannis,

thank you for the answer. Ok I did it and the output is the following (image attahced): So the imagelist is the FacesR and it just shows up the content of the list which is 80 images. And the type output is str. And then I just use the FacesR.pop() to show the images. And online the same thing (create a list of images and pop) it works during the training but not during the actual task. The only difference between them is that in the task I multiply the list 4 times so the training code is:

Python : FacesT= [f’FaceT_{i}.png’ for i in range(1,9)]

JS: FacesT = function () {
var _pj_a = , _pj_b = util.range(1, 9);
for (var _pj_c = 0, _pj_d = _pj_b.length; (_pj_c < _pj_d); _pj_c += 1) {
var i = _pj_b[_pj_c];
_pj_a.push(FaceT_${i}.png);
}
return _pj_a;
}
.call(this);

While during the task the code is:

Py: FacesR= [f’Face_{i}.png’ for i in range(1,21)] * 4;

JS: FacesR = (function () {
var _pj_a = , _pj_b = util.range(1, 21);
for (var _pj_c = 0, _pj_d = _pj_b.length; (_pj_c < _pj_d); _pj_c += 1) {
var i = _pj_b[_pj_c];
_pj_a.push(Face_${i}.png);
}
return _pj_a;
}
.call(this) * 4);

I do not know why the error pop is not a function comes up only during the task.
Thanks in advance for any tip.

Best,
anastasia

Hi @Anastasia_Dimakou,

The reason why I asked to report on the output of console.log('type of imagelist = ', typeof imagelist); instead of print is because though your python code may work just fine locally, the attempted automatic translation to JS is not always correct and hence give you issues when you try to run it online.

Indeed, the output of console.log('type of imagelist = ', typeof imagelist); is NaN. And shift() cannot be implemented on NaN.

The issue appears to be with FacesT= [f’FaceT_{i}.png’ for i in range(1,9)]*4.- I do not why, I am not familiar with JS.

Try the following python code instead:

FacesT0= [f'FaceT_{i}.png' for i in range(1,9)]
FacesT = []
for i in range(4):
    for j in FacesT0:
        FacesT.append(j)
// FacesT.shift() should now work

The above should be translated automatically and correctly to JS.

Hope this heslp

BW,
Yiannis

1 Like

From the manual translation section of my crib sheet

Python
Using multiplication to create a larger array, e.g. [0, 1] * 10

JS
Array(10).fill([0,1]).flat(); dvbridges

2 Likes

Hello Yiannis,
Thank you very much for the help. Yes I realized that the problem is with the multiplication. This code actually works so images starting to show but then at some point they stop and there is a super weird error. Maybe I will try with the array solution proposed below.

thanks again,
Anastasia

That error us due to you either having pasted images into your Excel files (as opposed to image file names), of you are using a name for your image variable that means something else. What is the image name used for image_8?

2 Likes

So in the image_8 I have a variable named $image_file and this is defined in the code in which I make the list of images. Then I just have an excel file with conditions so if the condition is F then take one image from the list Faces. I upload the images from the online => Additional sources.

Ok took a look again on the creeb shit and I realized I had a tif image inside the list that was causing the error! So now it works thanks a lot!

1 Like