Reputation: 55
While doing a find query using the "qnum" variable argument as a value it doesn't return any data. Is it due to the limitation of the variable scope inside an object?
Quiz.js file
Questions = new Mongo.Collection("questions");
if (Meteor.isClient) {
var qnum = 1;
Template.question.events({
"click #submit-btn": function (event, template) {
//Increment question number when user click on submit button which loads the next question
qnum += 1;
}
});
Template.body.helpers({
questions: function (qnum) {
return Questions.find({qnum: qnum});
}
});
}
Quiz.html file
<div class="quiz">
<div class="score-board">
{{#each questions}}
{{> question}}
{{/each}}
</div>
</div>
</body>
<template name="question">
<div class="qa-wrapper" id="{{qnum}}">
<div class="questions">{{title}}</div>
<div class="options">
{{#each options}}
<input type="checkbox" name="{{this}}"/>{{this}} <br />
{{/each}}
<button id="submit-btn">Submit</button>
</div>
</div>
</template>
Questions db collection
{ "_id" : ObjectId("55a207a21c4dbe14cf9837c7"), "qnum" : 1, "title" : "Question 1", "options" : [ "A", "B", "C", "D" ] }
{ "_id" : ObjectId("55a207b31c4dbe14cf9837c8"), "qnum" : 2, "title" : "Question 2", "options" : [ "A", "B", "C", "D" ] }
Upvotes: 1
Views: 1077
Reputation: 1807
my hunch is that when you setup an argument to your template helper questions(qnum)
it expects one, but then in the {{#each questions}}
bit you don't pass anything. try and remove the qnum
in in the template.helper def so that it becomes
Template.body.helpers({
questions: function () {
return Questions.find({qnum: qnum});
}
});
untested. let me know if it works.
To actually get your helper to rerun on button click, you need to make the query parameter (qnum) reactive. Here, Meteor offers various choices, the most obvious one would be to store your qnum
value in a Session.
Session.set("qnum", qnum)
and then in the template helper query
return Questions.find({qnum: Session.get("qnum") });
Upvotes: 1