psychopy.org | Reference | Downloads | Github

Python to JavaScript - Uncaught SyntaxError: Unexpected number

URL of experiment: https://run.pavlovia.org/Topor/football-game-probabilistic-learning/html/

Description of the problem: Hi! I am working again on my online tasks after a while. I have made a few changes to a task that ran on Pavlovia successfully before. I use the builder and there is quite a lot of hand-written code in it. I added new lines of Python code and the default automatic “translation” from Python to JS added the new changes in JS from what I can see. However, I am not sure whether this new automatic option could have also made changes to the other parts of code I “translated” myself before in the original version of the task. I am not very savvy with JS so it’s hard for me asses this - to an untrained eye it looks unchanged.

When trying to run the new version of the task on Pavlovia, I get the following errors:
Chrome: Uncaught SyntaxError: Unexpected number
Mozilla: SyntaxError: unexpected token: numeric literal

What I have done so far:

  • I realised that there were semi-colons missing in the JS code and I added these
  • I checked if brackets were complete and as far as I can see they are
  • I checked if quotation marks are complete and as far as I can see they are too
  • I looked for misplaced comas and dots but can’t identify any
  • I looked for any trailing 0 that could have crept into the code but there are non from what I can see

The error persists and I am not sure what could be causing it.

As always, all advice will be very appreciated, thanks!

It says the error is in line 5 which is

import { PsychoJS } from 'https://pavlovia.org/lib/core-'3.1.0'.js';

I think you need to upgrade. I’d recommend the latest version. That line in my experiments says

import { PsychoJS } from './lib/core-2020.1.js';


Thank you @wakecarter!

I have now upgraded to the suggested 2020.1. I removed the cached files from the browser and pushed all the changes. Line 5 now looks exactly as the one you showed from your experiment.

A similar error is displayed in the console: Uncaught SyntaxError: Unexpected token ‘{’

PsychoPy shows the lines where the error can be traced to:
21.8433 ERROR Line 326: Unexpected token in Football Game Probabilistic Learning.js
25.6772 ERROR Line 318: Unexpected token in Football Game Probabilistic Learning-legacy-browsers.js

It is the same line of code in both files:

`

trialClock = new util.Clock();

This is not part of the code I put in manually so I am wondering what could be still causing the error to persist? There is nothing suspicious here about this line, other lines with util.Clock() for other components of the task look the same.

Thanks!

I think that your issue is a missing } bracket prior to that line, almost certainly in a custom code component

The component that is initialised prior to “trial” is just an instructions one. The only custom code I added there is placed in line 299 and has no ‘{’ :

myCount = 0;

There is some more custom code in that component but it’s not run until line 1748 as it is in the “begin rutine box”.

myCount = myCount + 1;

if (myCount === 1){
    letterMaster.finished = true;
}

The line that directly proceeds the error (326) line is 323 and it is not custom code and looks like it does not have a bracket issue:

instr1_resp = new core.Keyboard({psychoJS: psychoJS, clock: new util.Clock(), waitForStart: true});

Sorry about this, it seems to be very tricky to localise the issue!

Last one I came across ended up being due to a formatted string

Thanks for this!

I just identified the issue! It was the line directly following the suggested error line (327):

import {random} from 'numpy/random';

I did not realise that this is something that is only used in Python. It’s there because I use jitter in the task. It was translated automatically. I removed it now and the task starts fine. :slight_smile:

I expect that there will still be more issues to debug now that the task is running :sweat_smile: