psychopy.org | Reference | Downloads | Github

TypeError: Undefined is not an object

URL of experiment

Description of the problem:

Like many others on this forum, my experiment runs in PsychoPy, but not in Pavlovia. The error I get is the one above, although the number changes per run. I have read the other threads with this topic but it seems to be something different every time. I have translated all the code to JS (with a lot of help from an awesome user on this forum) but to no avail.

The full error in Javascript is this (this time number 9):

core-2020.1.js:1437 TypeError: Cannot read property '9' of undefined
    at Scheduler._currentTask (Experiment_EliseAlberts.js:2002)
    at Scheduler._runNextTasks (util-2020.1.js:1091)
    at Scheduler._runNextTasks (util-2020.1.js:1094)
    at Scheduler._runNextTasks (util-2020.1.js:1094)
    at Scheduler._runNextTasks (util-2020.1.js:1094)
    at update (util-2020.1.js:1058)

Does anyone have any suggestions on how to solve this? Thank you!

Update, when I run the script in Safari, I get a more specific error message:

So I think something is wrong with the variable in my Text component shown below.

I wrote down

$SentBeforePrac_list[BeforePracLoop.thisN]

Is there a way to circumvent putting this code directly in the text component but instead using a custom code? That way Psychopy should automatically translate it to JS…

Hi,

Online, all loops need to be referred to as trials (not BeforePracLoop in your case)

Have a look at my crib sheet here:

Best wishes,

Wakefield

Thanks Wakefield, that certainly did something. It now breaks down when encountering this function:

TypeError: undefined is not an object (evaluating 'SentAfterPrac_list[trials2.thisN]')
(anonymous function) — Experiment_PavloviaProof.js:2441
_runNextTasks — util-2020.1.js:1091
_runNextTasks — util-2020.1.js:1094
_runNextTasks — util-2020.1.js:1094
_runNextTasks — util-2020.1.js:1094
update — util-2020.1.js:1058
(anonymous function) — core-2020.1.js:1437
log4javascript.min.js:1:40080
type or paste code here

But this is AFTER it shows me a few of the elements of the trial.

I have reuploaded my experiment under a different name which can be found here.

In Safari, it says this:

  • TypeError: SentAfterPrac.split is not a function. (In ‘SentAfterPrac.split(" ")’, ‘SentAfterPrac.split’ is undefined)

The document states something about inner loops - however I am not quite sure how to work around that…do you have a suggestion for that as well?

Thank you again for taking the time to help me!

Try trials.thisN instead of trials2.thisN

Ah, okay! I thought the reference needed to be to exactly the loop, and since I can’t name a loop ‘trials’ twice I named it trials2. Hmm.

It works in Psychopy but now for some reason this error is back.

ReferenceError: len is not defined

I put this in a code component before the loop starts (so before BeforeStimSentPrac) to make sure there is a reference:

afterSentLength = len(SentAfterPrac.split())

and the corresponding JS translation:

afterSentLength = SentAfterPrac.split().length;

and then $afterSentLength in the text box.

Do you think I need to put that somewhere else for Javascript to recognise the reference? Or is the translation wrong in some way?

Thanks again. I’ve been working on this for so long - no idea if I ever can get it to work online.

Hi Elise,

You call the loops different things in PsychoPy but then refer to them all as trials in PsychoJS.

Did you get len is not defined as an online error? It looks like len in Python is getting translated to .length in Javascript so I’m wondering whether there’s another reference to len somewhere in your code. I’ve been loading up the Javascript code from the git repository and then searching for the offending term.

I was beginning to lose hope with Ebbinghaus Darts but I think I’m now there and have learned a lot in the process. Some things won’t work but most of it seems to be fine once you get your head around it.

Yes I got it as an online error, and you’re right: I had one loop with still the len function. I implemented it everywhere and I’m getting closer! I am only getting this error again:

core-2020.1.js:1437 TypeError: SentAfterPrac.split is not a function

So it doesn’t recognise it as a JS function I guess. Which is strange because from what I can see online, this seems the way to do it. My experiment runs, but doesn’t split up the sentences into words like I wanted it to do - instead, it displays the full sentence (which is in the excel file). But still. I’m one step further!

Good to hear you got yours to work. Do you have any more suggestions for this problem?

Hi Again,

It looks there is at least one place in your code where you have SentAfterPrac.split() rather than SentAfterPrac.split(" "). This is probably why you get the whole sentence displayed rather than individual words in succession.

The TypeError .split is not a function that you are getting typically means that SentAfterPrac is not a string. So I would check the file that loads this variable and see how the trial that elicits this error might be different from the others.

Good luck!
Keith

1 Like

KEITH you are an absolute angel of a human being. IT WORKS.

Okay so I’ll summarise the issues I had to fix before it finally worked for others if they need a similar fix. Also here’s a quick summary of what the experiment is actually doing for those who are interested, and if anyone needs to create a similar one:

During the experiment, participants see words appearing one by one on the screen. Together, these words form a sentence. One word in the sentence is substituted by a picture. Next, a letter will appear in the middle of the screen. Their task is to indicate whether the sound of this letter is present in the picture name and they will be given feedback to this.

The excel list, thus, consists of a ‘sentencebefore’ list and a ‘sentenceafter’ list with what needs to appear before and after the picture and the letter. However, one problem that I JUST realised because of your comment, Keith, is that this means there are sometimes cells containing only ‘the’ or cells containing only an enter (because the sentence ends with the picture). This did work in Psychopy, which is why I was so confused when I got the error. What I did now, is I put words in every cell (right now it says ‘boo boo’ because that’s my mood) and now it does recognise it as a string. I do need to still find a solution how to create a ‘fake’ string of words that will not be seen by the participant but I think I’ll be able to find something.

Some things I did (with a lot of help from your replies) before it worked in Pavlovia:

  1. Replace all the custom names for loops to ‘trials’ in the Javascript code (code > both > edit the JS translation)
  2. Define the len(SentAfterPrac.split()) in one of the custom codes boxes first, so that the code included the JS translation (instead of putting it in the $rep in the loop options box) - and this is important for any code that you put directly in the variable box.
  3. The JS code for len(SentAfterPrac.split()) is indeed SentAfterPrac.split(" ").length; and crucially NOT SentAfterPrac.split().length; which is what PsychoPy automatically translates it to.

Thanks again Keith and Wakecarter! You’ve been a big help!

Thanks Keith. I’ve added your .split() fix to my crib sheet.

1 Like

And thank you too of course! I’m glad this community is so helpful :slight_smile:

Hooray! Thanks to everyone for being helpful and congrats on getting a running experiment online!

Hi Elise,
can I ask you some questions on the three points how you fixed the problem.
So the first thing to to is to add a loop to my routine which only consists of the image, the rating scale and a keyresponse to end the routine, right? The loop is only called trials and what should be clicked/inserted there. No condition file and for nreps? 1?
Then I add a code component to my routine. Selected both and then? What is the code?
I’m so sorry I’m a absolute newbie here

I have messaged you on your own thread as well but just in case: can you make your experiment files public so I can take a look?

Regards,

Elise