Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

Study session data returns undefined

edited June 2022 in JATOS

Hi,

I have been trying many different ways to use study session data in jsPsych unsuccessfully. I found that the only way I can access the jatos.studySessionData object is once the jatos.onLoad() function has been called. Otherwise, I always get an undefined object.


Here is my code simplified to show what I want to do:

<script>

    /* Initialize jsPsych*/
    var jsPsych = initJsPsych({
        show_progress_bar: true,
        on_trial_start: function () {
            jatos.addAbortButton({
                action: revert_batch
            });
        },
        on_finish: function() {
                jatos.startNextComponent(jsPsych.data.get().json());
        }
    });


    timeline = [];
    var start_idx;
    var end_idx;
    var block_prompt;
    var cat_stim_paths;
    var cat_stim;
    
    //Code that needs access to jatos.studySessionData
    if (jatos.studySessionData.last_block_num == 0){
            console.log('batch 1');
            start_idx = 0;
            end_idx = 2;
            block_prompt = "<h2>Block 1 of Part 2 of the experiment will now start. Press any key to begin.</h2>";
        } else if (jatos.studySessionData.last_block_num == 1) {
            console.log('batch 2');
            start_idx = 2;
            end_idx = 4;
            block_prompt = "<h2>Block 2 of Part 2 of the experiment will now start. Press any key to begin.</h2>";
        } else if (jatos.studySessionData.last_block_num == 2) {
            console.log('batch 3');
            start_idx = 4;
            end_idx = 6;
            block_prompt = "<h2>Block 3 of Part 2 of the experiment will now start. Press any key to begin.</h2>";
        } else {
            //always ends up here since jatos.studySessionData returns undefined
            console.log('batch 4');
            start_idx = 6;
            block_prompt = "<h2>Block 4 of Part 2 of the experiment will now start. Press any key to begin.</h2>";
        };
    
     // Get session data
     cat_stim_paths = jatos.studySessionData.cat_stim_paths; //this returns undefined
     cat_stim_paths = cat_stim_paths.slice(start_idx, end_idx); //and then this returns an error because undefined
     cat_stim = jatos.studySessionData.cat_stim;
     cat_stim = cat_stim.slice(start_idx, end_idx);
     
    /*
               LOGIC THAT USES SESSION DATA (ALL JSPSYCH STUFF)
    */
    
    /* start the experiment */
    jatos.onLoad(() => {
        jatos.addAbortButton({
            action: revert_batch
        });
    
        jsPsych.run(timeline);
        
    });
</script>

For context, cat_stim is a list of timeline objects and cat_stim_paths is the array of paths for pre-loading (which I can verify in the console has been saved to the study session data object).


Thanks for the help!

Comments

  • Hi,

    Yes, you can only access the study session after the jatos.onLoad function got called. How about you wrap everything with jatos.onLoad?

    <script>
        jatos.onLoad(() => {
    
            /* Initialize jsPsych*/
            var jsPsych = initJsPsych({
                show_progress_bar: true,
                on_trial_start: function () {
                    jatos.addAbortButton({
                        action: revert_batch
                    });
                },
                on_finish: function () {
                    jatos.startNextComponent(jsPsych.data.get().json());
                }
            });
    
            timeline = [];
            var start_idx;
            var end_idx;
            var block_prompt;
            var cat_stim_paths;
            var cat_stim;
    
            //Code that needs access to jatos.studySessionData
            if (jatos.studySessionData.last_block_num == 0) {
                console.log('batch 1');
                start_idx = 0;
                end_idx = 2;
                block_prompt = "<h2>Block 1 of Part 2 of the experiment will now start. Press any key to begin.</h2>";
            } else if (jatos.studySessionData.last_block_num == 1) {
                console.log('batch 2');
                start_idx = 2;
                end_idx = 4;
                block_prompt = "<h2>Block 2 of Part 2 of the experiment will now start. Press any key to begin.</h2>";
            } else if (jatos.studySessionData.last_block_num == 2) {
                console.log('batch 3');
                start_idx = 4;
                end_idx = 6;
                block_prompt = "<h2>Block 3 of Part 2 of the experiment will now start. Press any key to begin.</h2>";
            } else {
                //always ends up here since jatos.studySessionData returns undefined
                console.log('batch 4');
                start_idx = 6;
                block_prompt = "<h2>Block 4 of Part 2 of the experiment will now start. Press any key to begin.</h2>";
            };
    
            // Get session data
            cat_stim_paths = jatos.studySessionData.cat_stim_paths; //this returns undefined
            cat_stim_paths = cat_stim_paths.slice(start_idx, end_idx); //and then this returns an error because undefined
            cat_stim = jatos.studySessionData.cat_stim;
            cat_stim = cat_stim.slice(start_idx, end_idx);
    
            /*
                        LOGIC THAT USES SESSION DATA (ALL JSPSYCH STUFF)
             */
    
            /* start the experiment */
            jatos.addAbortButton({
                action: revert_batch
            });
    
            jsPsych.run(timeline);
        });
    </script>
    

    Best,

    Kristian

  • Wow thank you so much Kristian!

Sign In or Register to comment.