# Python to JS code error (TypeError)

**URL:** <https://discourse.psychopy.org/t/python-to-js-code-error-typeerror/34262>\
**Category:** Online experiments\
**Created:** [April 5, 2023, 2:20pm UTC](https://discourse.psychopy.org/t/python-to-js-code-error-typeerror/34262 "2023-04-05T14:20:56Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![MarinaIosifian](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/marinaiosifian/32/7507_2.png) [@MarinaIosifian](https://discourse.psychopy.org/u/MarinaIosifian)\
**Post date:** [April 5, 2023, 2:20pm UTC](https://discourse.psychopy.org/t/python-to-js-code-error-typeerror/34262/1 "2023-04-05T14:20:56Z")

</div>

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:**

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

current_word = sentence_list.pop(0)

```

**JS:**

```auto
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.

---

<div class="post-metadata">

**Author:** ![JensBoelte](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jensboelte/32/9060_2.png) [@JensBoelte](https://discourse.psychopy.org/u/JensBoelte)\
**Post date:** [April 5, 2023, 3:21pm UTC](https://discourse.psychopy.org/t/python-to-js-code-error-typeerror/34262/2 "2023-04-05T15:21:45Z")

</div>

Hello,

this toy-experiment runs offline and online. It presents numbers 0 - 5 in randomized order.  
[PopShift.psyexp](https://discourse.psychopy.org/uploads/short-url/vvIJBnpH2zKbTKcwiqY6mBUeaeG.psyexp) (11.4 KB)

Hope that helps

Best wishes Jens

---

<div class="post-metadata">

**Author:** ![MarinaIosifian](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/marinaiosifian/32/7507_2.png) [@MarinaIosifian](https://discourse.psychopy.org/u/MarinaIosifian)\
**Post date:** [April 5, 2023, 3:37pm UTC](https://discourse.psychopy.org/t/python-to-js-code-error-typeerror/34262/3 "2023-04-05T15:37:56Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![JensBoelte](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jensboelte/32/9060_2.png) [@JensBoelte](https://discourse.psychopy.org/u/JensBoelte)\
**Post date:** [April 5, 2023, 4:16pm UTC](https://discourse.psychopy.org/t/python-to-js-code-error-typeerror/34262/4 "2023-04-05T16:16:34Z")

</div>

Hello,

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

[sentStim.psyexp](https://discourse.psychopy.org/uploads/short-url/ds4oADPiHKmUtiWbkpouuf4AXcP.psyexp) (11.4 KB)  
[sentStim.xlsx](https://discourse.psychopy.org/uploads/short-url/km8dcwhTN6Eb6sqUU1GYjvZneIf.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

---

<div class="post-metadata">

**Author:** ![MarinaIosifian](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/marinaiosifian/32/7507_2.png) [@MarinaIosifian](https://discourse.psychopy.org/u/MarinaIosifian)\
**Post date:** [April 5, 2023, 5:21pm UTC](https://discourse.psychopy.org/t/python-to-js-code-error-typeerror/34262/5 "2023-04-05T17:21:17Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/3/8/3849ef038a5740bbc0d024b7dc55611dd9caa631.png)

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [April 6, 2023, 6:05am UTC](https://discourse.psychopy.org/t/python-to-js-code-error-typeerror/34262/6 "2023-04-06T06:05:14Z")

</div>

Try putting sentence\_list = in Begin Experiment

---

<div class="post-metadata">

**Author:** ![JensBoelte](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jensboelte/32/9060_2.png) [@JensBoelte](https://discourse.psychopy.org/u/JensBoelte)\
**Post date:** [April 6, 2023, 9:14am UTC](https://discourse.psychopy.org/t/python-to-js-code-error-typeerror/34262/7 "2023-04-06T09:14:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![MarinaIosifian](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/marinaiosifian/32/7507_2.png) [@MarinaIosifian](https://discourse.psychopy.org/u/MarinaIosifian)\
**Post date:** [April 6, 2023, 9:21am UTC](https://discourse.psychopy.org/t/python-to-js-code-error-typeerror/34262/8 "2023-04-06T09:21:25Z")

</div>

Do you mean?

```auto

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](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/1/e/1e5af8d1dfa7199599fd263ec66873682230cec5.png)

---

<div class="post-metadata">

**Author:** ![JensBoelte](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jensboelte/32/9060_2.png) [@JensBoelte](https://discourse.psychopy.org/u/JensBoelte)\
**Post date:** [April 6, 2023, 9:34am UTC](https://discourse.psychopy.org/t/python-to-js-code-error-typeerror/34262/9 "2023-04-06T09:34:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![MarinaIosifian](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/marinaiosifian/32/7507_2.png) [@MarinaIosifian](https://discourse.psychopy.org/u/MarinaIosifian)\
**Post date:** [April 6, 2023, 10:17am UTC](https://discourse.psychopy.org/t/python-to-js-code-error-typeerror/34262/10 "2023-04-06T10:17:51Z")

</div>

Hello,

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

---

<div class="post-metadata">

**Author:** ![JensBoelte](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jensboelte/32/9060_2.png) [@JensBoelte](https://discourse.psychopy.org/u/JensBoelte)\
**Post date:** [April 6, 2023, 10:31am UTC](https://discourse.psychopy.org/t/python-to-js-code-error-typeerror/34262/11 "2023-04-06T10:31:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![MarinaIosifian](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/marinaiosifian/32/7507_2.png) [@MarinaIosifian](https://discourse.psychopy.org/u/MarinaIosifian)\
**Post date:** [April 6, 2023, 6:01pm UTC](https://discourse.psychopy.org/t/python-to-js-code-error-typeerror/34262/12 "2023-04-06T18:01:24Z")

</div>

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](https://discourse.psychopy.org/t/error-when-importing-conditions-in-nested-loops/14070)

---

<div class="post-metadata">

**Author:** ![JensBoelte](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jensboelte/32/9060_2.png) [@JensBoelte](https://discourse.psychopy.org/u/JensBoelte)\
**Post date:** [April 7, 2023, 9:46am UTC](https://discourse.psychopy.org/t/python-to-js-code-error-typeerror/34262/13 "2023-04-07T09:46:32Z")

</div>

Hello,

yes, it runs online. Give it a [try](https://run.pavlovia.org/jens.boelte/sentstim).

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

---

<div class="post-metadata">

**Author:** ![Chen](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/chen/32/38825_2.png) [@Chen](https://discourse.psychopy.org/u/Chen)\
**Post date:** [April 7, 2023, 10:06am UTC](https://discourse.psychopy.org/t/python-to-js-code-error-typeerror/34262/14 "2023-04-07T10:06:56Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![MarinaIosifian](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/marinaiosifian/32/7507_2.png) [@MarinaIosifian](https://discourse.psychopy.org/u/MarinaIosifian)\
**Post date:** [April 7, 2023, 1:05pm UTC](https://discourse.psychopy.org/t/python-to-js-code-error-typeerror/34262/15 "2023-04-07T13:05:41Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![JensBoelte](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jensboelte/32/9060_2.png) [@JensBoelte](https://discourse.psychopy.org/u/JensBoelte)\
**Post date:** [April 7, 2023, 1:09pm UTC](https://discourse.psychopy.org/t/python-to-js-code-error-typeerror/34262/16 "2023-04-07T13:09:10Z")

</div>

Hello,

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

Best wishes Jens

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [April 7, 2023, 6:04pm UTC](https://discourse.psychopy.org/t/python-to-js-code-error-typeerror/34262/17 "2023-04-07T18:04:49Z")

</div>

The trials.thisN variable was fixed a while ago.

---

<div class="post-metadata">

**Author:** ![Chen](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/chen/32/38825_2.png) [@Chen](https://discourse.psychopy.org/u/Chen)\
**Post date:** [April 7, 2023, 11:58pm UTC](https://discourse.psychopy.org/t/python-to-js-code-error-typeerror/34262/18 "2023-04-07T23:58:30Z")

</div>

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 😅

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [April 8, 2023, 8:04am UTC](https://discourse.psychopy.org/t/python-to-js-code-error-typeerror/34262/19 "2023-04-08T08:04:26Z")

</div>

According to my crib sheet it was fixed in 2021.2
