Problems accesing BatchSessionData
Dear JATOS community,
I am encountering issues with accessing JATOS Batch Session Data. For context, I am using an HTML script with jsPsych to run an online task, and my university uses JATOS Server (Version 3.5.11).
I need to assign a unique participant ID to each person entering the experiment and track this information efficiently to avoid repeating IDs. I thought I could solve this by storing the available participant IDs, temporarily taken participant IDs, and fully taken participant IDs in the JATOS Batch Session data.My plan was to check if a list called “assignedIDs” exists in Batch Session. If it’s not defined (i.e., the first participant), I would initialize the list from a file called “sub_list”. If it’s already defined, I would use the list in Batch Session.Then, I would randomly select an ID from the “free” list, move it to the “temporary taken” list, and update the Batch Session data. This ID would then be used to load the study’s design matrices (via “loadDesignMatrices”).
The problem is that the code is not saving the information in the Batch Session data, so it cannot be accessed when a second participant runs the task. I understand that I might be mixing synchronous and asynchronous functions, but I need to obtain the design matrices at that exact moment to ensure the rest of the experiment runs smoothly (all the experiment depends on having the design_matrices of that partcipant loaded).
Below, I’ve included the relevant functions. If anyone has any suggestions on how to resolve this issue, I would be very grateful.
Thanks,
Paula
// load design matrices function loadDesignMatrices(subject_id) { var key = "sub_" + subject_id; console.log("Loading design matrices for key:", key); design_matrix = DM[0][key]; // Store in global variable pr_design_matrix = pr_DM[0][key]; // Store in global variable console.log("Design matrix loaded:", design_matrix); console.log("Practice design matrix loaded:", pr_design_matrix); } // Assign random subject id from the available ids function assign_subject() { console.log("Starting assign_subject_id..."); let assignedIDs; if (jatos.batchSession.get("assignedIDs") === undefined) { assignedIDs = { free: [...sub_list[0].free], temp: Array.isArray(sub_list[0].temp) ? [...sub_list[0].temp] : [], taken: Array.isArray(sub_list[0].taken) ? [...sub_list[0].taken] : [] }; } else { console.log(jatos.batchSession) // Retrieve assignedIDs from batch session using .then() jatos.batchSession.get("assignedIDs") .then(fetchedAssignedIDs => { assignedIDs = fetchedAssignedIDs; }) } console.log("trying", assignedIDs) if (assignedIDs.free.length > 0) { var randomIndex = Math.floor(Math.random() * assignedIDs.free.length); var subject_id = assignedIDs.free.splice(randomIndex, 1)[0]; // Remove from free list assignedIDs.temp.push(subject_id); // Move to temp console.log("Randomly moved subject ID to temp:", subject_id); // Load design matrices according to ID loadDesignMatrices(subject_id); // Return necessary data return { design_matrix, pr_design_matrix, subject_id }; // Save updated assignedIDs to batch session using .then() and .catch() jatos.batchSession.set("assignedIDs", assignedIDs) .then(() => { console.log("Updated assignedIDs in batch session:", assignedIDs); }) .catch(err => { console.error("Error updating batch session data:", err); }); } else { console.log("No available subject IDs in free."); } } // Call function assign_subject();
Comments
Hi Paula!
I formatted your code to make it better readable. I'd like to encourage everyone to format their code too.
First, but maybe you do this already, you can access `jatos.batchSession` only after the jatos.js got initialized. So you have to use `jatos.onLoad` to do that.
Then, when I look at your code, the last part where you set something in the batch session is never executed because you return before. I'd move the return statement inside the `jatos.batchSession.set`, the part where you handle the successful handing with then . Additional I'd do a recursive call to
assign_subject
in case it fails. All together it could look like this (but I never tested the code):I hope this helps.
Best,
Kristian