Python to JS code error (TypeError)

I am using a code component to break a sentence (sentence variable from my conditions file, containing a list of sentences) and present it word-by-word. This code works fine offline, but gives an error online:

  • TypeError: Cannot read properties of undefined (reading ‘shift’)

To translate my Python code to JS, I made some manual changes using @wakecarter crib sheet. However, this does not help:

Python:

if trials.thisN == 0:
    sentence_list = sentence.split()

current_word = sentence_list.pop(0)

JS:

if ((trials.thisN === 0)) {
    sentence_list = sentence.split(" ");
}
current_word = sentence_list.shift();

If anyone can show me how to correct the error in my JS code, it will be much appreciated.

Hello,

this toy-experiment runs offline and online. It presents numbers 0 - 5 in randomized order.
PopShift.psyexp (11.4 KB)

Hope that helps

Best wishes Jens

Hi,

Thank you very much for trying to help, this is much appreciated! I am really struggling with this experiment.
I am not sure, but maybe the error develops because JS does not understand that my variable is an array. In your experiment, in JS you defined numbers (num) as an array containing numbers. I have a similar variable, but I’m not sure how to define it as an array. Because it’s an excel column containing 40 different sentences, not one array I can define in my code component.

Hello,

ok, another approach. This ran locally in a browser.

sentStim.psyexp (11.4 KB)
sentStim.xlsx (8.4 KB)

It presents a sentence word by word if that is what you want to achieve. I am loosing track of your experiments. Let’s focus on one.

Best wishes Jens

Hi again,
For some reason, this experiment keeps presenting “Hello World” instead of the sentences in your xlsx file

Try putting sentence_list = in Begin Experiment

Hello Marina

That’s interesting because the experiment runs as intended, offline as a Python-based experiment, offline as a JavaScript-based experiment in a browser, and online. I have just tested it. So I don’t know why the script doesn’t work in your case. By the way, I am using 2022.2.4.

Best wishes Jens

Do you mean?


sentence_list = [];

Thanks! I also switched to 2022.2.4, I am not getting the error now, but instead of presenting my sentences, Pavlovia presents “Hello world”, which is extremely strange.

Can this be related to how I store my “sentence” variable in xlsx file?
image

Hello

No, I don’t think so. Did you look at my toy-example? Did my toy-experiment run? When yes, just change the Excel-file in my toy-experiment and give it a try.

Best wishes Jens.

Hello,

Running your example experiment also gives me “Hello world” on the screen, instead of the sentences in your xlsx file

Hello,

I have no idea. The program runs fine here.

Did you clear the cache or opened the experiment in a private browser window?

Best wishes Jens

are you sure it runs online? After searching the hello world issue on this forum, I found that variables defined in the conditions files can not be manipulated online. That is, my variable “sentence” cannot be split into words online: Error when importing conditions in nested loops

Hello,

yes, it runs online. Give it a try.

Well, in my script, the variable sentence that is read in is not modified. This was different in the post you refer to.

Best wishes Jens

Which version are you using?
As much as I know, trials.thisN doesn’t work on older versions online (not sure about new versions).
What you can do is create a new variable that is equal to 0. Then each time the routine start/end, increase this variable by one.

1 Like

Thank you for all your help!
I’ve been giving it a try all day yesterday, and it only worked today, when I uploaded (not switched to) and installed the newest PsychoPy version…

1 Like

Hello,

I am glad to read that it is now working for you.

Best wishes Jens

1 Like

The trials.thisN variable was fixed a while ago.

1 Like

I know that people still use 2020.2.10, which doesn’t seem to work.
It will be nice to know when exactly it was fixed :sweat_smile:

1 Like

According to my crib sheet it was fixed in 2021.2

1 Like