Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No condition field provided when calling condition API #91

Open
adityac95 opened this issue Nov 29, 2023 · 2 comments
Open

No condition field provided when calling condition API #91

adityac95 opened this issue Nov 29, 2023 · 2 comments

Comments

@adityac95
Copy link

This is the provided code to request the next condition number if not using jsPsych:

const response = await fetch("https://pipe.jspsych.org/api/condition/", {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    Accept: "*/*",
  },
  body: JSON.stringify({
    experimentID: "myExpIDHere",
  }),
});

However, though I can get the response, the condition field does not appear to be in the JSON. I keep getting the error "Cannot read properties of undefined" when I use the value response.condition to index into an array of four elements.

@adityac95 adityac95 changed the title No condition field provided when calling this function No condition field provided when calling condition API Nov 29, 2023
@jodeleeuw
Copy link
Member

Hi @adityac95 ,

Can you provide the surrounding context code as well? Is it in an async function?

@adityac95
Copy link
Author

adityac95 commented Nov 29, 2023

Yes. The relevant code is:

const conditionPairs = [
	['note','medium'],
	['short','long'],
	['note', 'long'],
	['short','medium'],
];

let participantMelodies = [];
let participantRatings = [[], []];
let currTrialNum = 0;
let currRatingNum = 0;
let trialConditions;
let currCondition;
let currMelodyToRate = '';
let emailId = '';

// get condition assignment
async function getCondition() {
	const response = await fetch("https://pipe.jspsych.org/api/condition/", {
		method: "POST",
	  	headers: {
	    	"Content-Type": "application/json",
	    	Accept: "*/*",
	  	},
	  	body: JSON.stringify({
	    	experimentID: "myExpIDHere",
	  	}),
	});
	console.log(response);
	return response
}

async function initExperiment() {
    let response;
    try {
        response = await getCondition();
    } catch (error) {
        console.error("Error fetching condition:", error);
        // Fallback to a random condition if fetch fails
        response = { condition: Math.floor(Math.random() * conditionPairs.length) };
    }

    trialConditions = conditionPairs[response.condition];

    // Set up the initial trial
    setupInitialTrial();
}

function setupInitialTrial() {
	// set the condition
    currCondition = trialConditions[currTrialNum];
}

// Call this function to start the experiment
initExperiment();

The TypeError is raised when setupInitialTrial() is called inside the initExperiment() function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants