Uncaught TypeError: cannot read property 'trial' of undefined

955 views
Skip to first unread message

Laurent

unread,
Jun 30, 2016, 3:15:26 PM6/30/16
to jsPsych
Hello everyone,

I'm new to jsPsych and JS in general. Currently we are building a relatively straightforward experiment in which the participant is presented with a serie of word-pairs out of which they have to choose one. The words are retrieved from a server and have a number of attributes. The script is made in such a way that we can easily adjust the experiment and module types we want to retrieve.

The instructions are displayed without any problems. However, when i want to present the words on screen i keep bumping into the same error messages:

Trial level node is missing the "type" parameter. The parameters of the node are: {//OBJECT PROPERTIES//}

followed by

Uncaught TypeError: cannot read property 'trial' of undefined

Any help on how to continue from here is much appreciated, the relevant chunk of code is this:

// Call function to collect experiment module
getData({"experiment":"1","module": "1"}, function(){
// retrieve questions from sample
var questions = this.questions;
var trials_exp1 = [];
// loop through these questions and create stimuli array
for (var i = 0; i < questions.length; i++) {
trials_exp1.push({
type: 'single-stim',
stimuli: [questions[i].question1 + " " + questions[i].question2],
choices: ['a','s'],
is_html: true
});
}
console.log("Questions for Experiment 1 finished loading");
// Push retrieved experiment to timeline
timeline.push(trials_exp1);
}).done (function(){
// all experiments are loaded, start the experiments
console.log("Finished loading: Start experiment");
startExperiment();
})

Thank you in advance!
- Laurent

Josh de Leeuw

unread,
Jul 1, 2016, 5:15:13 PM7/1/16
to Laurent, jsPsych
Hi Laurent,

The problem is how you are adding the trials_exp1 array to the timeline. Using push will add the array, but really you want to add all of the items in the array. 

Try: timeline = timeline.concat(trials_exp1);

Josh

--
You received this message because you are subscribed to the Google Groups "jsPsych" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jspsych+u...@googlegroups.com.
To post to this group, send email to jsp...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jspsych/71cabbf5-21ef-4486-8550-776aceee7204%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

l.s....@rug.nl

unread,
Sep 20, 2016, 12:06:59 PM9/20/16
to jsPsych
Hi Josh,

I had to drop the project for a while so my apologies for the late reply. Thank you for the response, concatenating did help. Unfortunately, the following error occurs when i try to test the experiment:

Uncaught ReferenceError: timeline is not defined
startExperiment @ IAT.html:110
(anonymous function) @ IAT.html:89
j @ jquery.min.js:2
fireWith @ jquery.min.js:2
x @ jquery.min.js:4
b @ jquery.min.js:4

While the problem seems to be obvious, i have trouble coming up with a solution. I'm using some helper functions throughout the script and it looks like the created timeline is not properly passed on to the jsPsych.init function. Also, when i log the timeline throughout the experiment it appears as an object, whereas in other experiments (for example the reaction time experiment from the tutorial) the timeline is logged as an array.

Do you have any idea what the problem might be? The stimuli are loaded in the code below

// Call function to collect experiment module
getData({"experiment":"1","module": "1"}, function(){
// retrieve questions from sample
var questions = this.questions;
var trials_exp1 = [];
// loop through these questions and create stimuli array
for (var i = 0; i < questions.length; i++) {
trials_exp1.push({
type: 'single-stim',

stimuli: ['<p style="text-align:center; font-size:32px;">'+questions[i].question1 + " "
+ questions[i].question2+'</p>'],


choices: ['a','s'],
is_html: true
});
};
console.log("Questions for Experiment 1 finished loading");

timeline = timeline.concat(trials_exp1);


}).done (function(){
// all experiments are loaded, start the experiments
console.log("Finished loading: Start experiment");
startExperiment();
})

And the experiment is started in this chunk:

// Start the experiment
function startExperiment(){
jsPsych.init({
timeline: timeline,
on_trial_start: function() {
console.log("Trial started");
},
on_finish: function() {
console.log("Experiment finished");
jsPsych.data.displayData('csv');

Josh de Leeuw

unread,
Sep 21, 2016, 9:08:18 AM9/21/16
to l.s....@rug.nl, jsPsych
You probably need to declare timeline in the global scope. You could try adding:

var timeline = [];

at the top level of the code (not in any function).

--
You received this message because you are subscribed to the Google Groups "jsPsych" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jspsych+u...@googlegroups.com.
To post to this group, send email to jsp...@googlegroups.com.

lauren...@gmail.com

unread,
Sep 30, 2016, 10:48:52 AM9/30/16
to jsPsych
Hi Josh,

Thank you, it was indeed an issue with the scope. Unfortunately, i'm already running into the next noobish issue: I want to present wordpairs that are gathered from a server when the experiment starts and they are concatenated into an array. When i try to present the words using the single-stim plugin the script apparently searches for the stimuli in my root folder:

ET file:///C:/Users/jsPsych%20script/[STIMULUS] net::ERR_FILE_NOT_FOUND

However, when i use the categorize plugin, the words are presented correctly and the paradigm runs fine. Am i missing something with the single-stim plugin? It's fine to use the categorize plugin but i feel that it is somewhat slower while my paradigm depends on a very fast and rhythmic presentation and responses. For example, is there a build-in parameter that turns off the feedback logging and presentation?

Finally, i also want to randomize the location of the word-pairs (e.g.: word 1 presented right and word 2 left and vice versa). I've thought of creating a copy of the stimulus array and switch the location in the second array. Then present a word-pair from a random stimulus array. But how do i avoid having the same pair presented twice? The second option i thought of was creating two presentation fields, one for each word, and randomize the location of those fields everytime a word-pair is presented. Does anyone have suggestions or knows an much easier way to work around it?

Again, thank you for your time!

Best,
Laurent

Josh de Leeuw

unread,
Oct 3, 2016, 9:08:21 AM10/3/16
to lauren...@gmail.com, jsPsych
Hi, Laurent.

If the word pairs are just words and not image files, then you should set the is_html parameter to true. You might still see the error message in the console, but it should work just fine. That error message is because jsPsych tries to preload the stimuli, but can't preload HTML content (and doesn't need to). Those errors are harmless (and no longer show up in version 6).

For the location issue, I think my strategy would be to create a list of all the pair-location combinations that I was interested in, so that the location is built in. This sounds like your first proposed solution.

Josh

--
You received this message because you are subscribed to the Google Groups "jsPsych" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jspsych+u...@googlegroups.com.
To post to this group, send email to jsp...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages