Reference Error: stimfile is not defined (using variable for stimfile)

Description of the problem:

My spreadsheet is not being defined. Using Newest PsychoPy version, experiment created using Builder, now trying to run on Pavlovia.

In the Begin Experiment tab of a code snippet in the first routine of my experiment (no loop around it), I have written code to randomly select a number 1-3, which will be attached to a filepath in order to randomly select a stimulus spreadsheet to use in some loops throughout this experiment (stimfile1, stimfile2, stimfile3- all uploaded in the Resources tab). This code is shown below and seems to work, as a console.log(stimlistFile) outputs the correct path (designspec/stimfile1.csv, for example).

randint = function(min, maxplusone) {
    return Math.floor(Math.random() * (maxplusone-min)) + min;
    }
var randomNum = randint(1, 4);
console.log(randomNum);
var spec = 'designspec/stimlist';
var csv = '.csv';
var stimlistFile = ('designspec/stimlist' + randomNum + '.csv');
console.log(stimlistFile);

However, when run in Pavlovia my experiment reaches the first loop that uses $stimlistFile and I immediately get an error: “ReferenceError: stimlistFile is not defined”. This error is traced to the section of my JS code involving the TrialHandler in the trialList line below.

trials_2 = new TrialHandler({
      psychoJS: psychoJS,
      nReps: 1, method: TrialHandler.Method.RANDOM,
      extraInfo: expInfo, originPath: undefined,
      trialList: stimlistFile,  
      seed: undefined, name: 'trials_2'
    });

I have tried:

  • playing with the format of stimlistFile creation (using String() around randomNum, using variables for the rest of the path, etc)
  • renaming all my loops from custom names to trial, trial_2, etc
  • defining the variable at the beginning of this code snippet (var stimlistFile;)
  • checking that all my resources are being uploaded

What else could be causing stimlistFile to remain undefined?
(please advise on how to provide link to code if necessary!)