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