Template.quiz.helpers({
//add you helpers here
'questionList': function () {
return Questions.find({},{limit: 1}).fetch();
}
});
Template.quiz.events({
'click .next': function (evt) {
evt.preventDefault();
}
});
database
{
"_id": “kAKbfLRHFZinrrRZv”,
“selected_subject”: “English”,
“question”: “ekek”,
“ans_A”: “e”,
“ans_B”: “q”,
“ans_C”: “t”,
“ans_D”: “o”,
“correctAns”: “e”
}
Client:
// If you've removed autopublish using `meteor remove autopublish`, uncomment the next line
// Meteor.subscribe('questions');
Session.setDefault('questionNumber', 1);
Template.quiz.helpers({
//add your helpers here
'questionList': function () {
return Questions.findOne({questionNumber: Session.get('questionNumber')});
}
});
Template.quiz.events({
'click .next': function (evt) {
evt.preventDefault();
var currentQuestionNumber = Session.get('questionNumber');
Session.set('questionNumber', currentQuestionNumber + 1);
}
});
Server: (If you’ve removed autopublish using meteor remove autopublish
, put the following in /server/publish.js
)
Meteor.publish('questions', function () {
return Questions.find();
});
Database:
{
"_id": "kAKbfLRHFZinrrRZv",
"selected_subject": "English",
"question": "ekek",
"ans_A": "e",
"ans_B": "q",
"ans_C": "t",
"ans_D": "o",
"correctAns": "e",
"questionNumber": 1
}
Give each question its own questionNumber
field, with values 1, 2, 3, …
Thank you @babrahams, please how do i add a questionNumber field to my meteor db and make it auto increment by default?
When you add a question, you will do something like this:
CLIENT AND SERVER:
Meteor.methods('addNewQuestion', function (newQuestionData) {
check(newQuestionData, Object);
check(newQuestionData.question, String);
// Plus various other checks
Questions.insert(newQuestionData);
}
where the newQuestionData
is something like this:
{
"selected_subject": "English",
"question": "ekek",
"ans_A": "e",
"ans_B": "q",
"ans_C": "t",
"ans_D": "o",
"correctAns": "e"
}
Change the addNewQuestion
method to:
Meteor.methods('addNewQuestion', function (newQuestionData) {
check(newQuestionData, Object);
check(newQuestionData.question, String);
// Plus various other checks
var existingQuestionCount = Questions.find().count();
newQuestionData.questionNumber = existingQuestionCount + 1;
Questions.insert(newQuestionData);
}