JavaScript function does not work with Wix online with MCQs

I wrote a function using JavaScript that calculates score for the MCQ and shows the correct answers once a button is pressed. I have the function work in the preview version but does not work in live.
Thanks.

Hi Shrijesh,

I have no idea what an MCQ is, but If it uses a database collection you need to sync the sandbox withg the Live:


Good luck,

Yisrael

Hey Yisarel, I’m sorry about the confusion, I meant multiple choice questions. It takes the given values from radio buttons and compares them to values stores in the rows of a database. I tried syncing it with the sandbox like you said, but still no result.
Thanks.

Hi Shrijesh,

I really need more details on what you are doing to be able to help you. How are you accessing the collection? What is the code you are using? What does your collection look like?

Yisrael

This is what my code looks like and how i am accessing the collection:

export function btnSubmit_click(event, $w) {
//Add your code for this event here:
//reads from the dataset Quizzes and checks the value from Radio buttons,
// if correct, the scoreCount increases by 1.
var scoreCount =0;
wixData.query(“Quizzes”)
.eq(“chapters”, “1”)
.find()
.then( (results) => {
let chapter = results.items;
if ($w(“#radioGroup1”).value === chapter[0].ques_1){scoreCount +=1;}
if ($w(“#radioGroup2”).value === chapter[0].ques_2){scoreCount +=1;}
if ($w(“#radioGroup3”).value === chapter[0].ques_3){scoreCount +=1;}
if ($w(“#radioGroup4”).value === chapter[0].ques_4){scoreCount +=1;}
if ($w(“#radioGroup5”).value === chapter[0].ques_5){scoreCount +=1;}
if ($w(“#radioGroup6”).value === chapter[0].ques_6){scoreCount +=1;}
if ($w(“#radioGroup7”).value === chapter[0].ques_7){scoreCount +=1;}
if ($w(“#radioGroup8”).value === chapter[0].ques_8){scoreCount +=1;}
if ($w(“#radioGroup9”).value === chapter[0].ques_9){scoreCount +=1;}
if ($w(“#radioGroup10”).value === chapter[0].ques_10){scoreCount +=1;}
if ($w(“#radioGroup11”).value === chapter[0].ques_11){scoreCount +=1;}
if ($w(“#radioGroup12”).value === chapter[0].ques_12){scoreCount +=1;}
if ($w(“#radioGroup13”).value === chapter[0].ques_13){scoreCount +=1;}
if ($w(“#radioGroup14”).value === chapter[0].ques_14){scoreCount +=1;}
if ($w(“#radioGroup15”).value === chapter[0].ques_15){scoreCount +=1;}
if ($w(“#radioGroup16”).value === chapter[0].ques_16){scoreCount +=1;}
if ($w(“#radioGroup17”).value === chapter[0].ques_17){scoreCount +=1;}
if ($w(“#radioGroup18”).value === chapter[0].ques_18){scoreCount +=1;}
if ($w(“#radioGroup19”).value === chapter[0].ques_19){scoreCount +=1;}
if ($w(“#radioGroup20”).value === chapter[0].ques_20){scoreCount +=1;}
$w(“#Score”).text = scoreCount.toString();
// shows previously hidden elements.
$w(“#Score”).show();
$w(“#textScore”).show();
$w(“#textScore2”).show();
$w(“#ansKey”).show();
$w(“#answers”).show();

} )
.catch( (err) => {
let errorMsg = err;
} );
}

My table looks like the following


This is how the page should work and is correctly working in Preview


But it does not work in live as expected.
Thanks for your help.

Make sure you sync your sandbox database to the live database:

Okay i think i see what has been going wrong, the order changes in the sandbox while syncing and its not the same as it is in the collection. I will try fixing that and update you on how it goes. Thanks.

Hi @Shrijesh I am trying to do almost the same quiz form and looks like I can borrow your code. May I know what is that “chapter” you have in your database? The database ques_1 etc… contains the correct answer right? and once the user clicks the correct answer it gives the value of the score. I just would like to know what was the chapter so that I could do mine based from your code. I am not a coder and I was so pleased to see similar format.

Hi @Analiza, sure you can use the code, I had multiple chapters in the web page so I was trying to use the database to contain correct answers to each of the chapter. And yes ques_1 is the correct answer. In my case, the quiz and the website worked perfectly in preview but did not work out in the published website . Please let me know if you get the same results or if it works out for you .Thanks