I am on a quest to showcase groups of 3 documents from my "Questions" collection, which contains about 50 documents. The plan is to display the first set of 3 questions in the template and then provide the users with an option (by clicking a button) to either fetch the next group of 3 documents or wrap up.
So far, I've hit a roadblock in loading the questions in batches of 3. Here's how my code looks:
collections.js:
Questions = new Mongo.Collection("questions");
myapp.js:
var lastQ=0
Template.questions.helpers ({
getGroupQuestions: function(){
//My goal here is to query the entire collection just once and store it in a local variable
var listOfQuest = Questions.find({$and: [ {qNumber: {$nin: answeredQ}}, {qNumber:{$gt:lastQ}}]}, {sort:{qNumber:1}});
lastQ = lastQ + 3;
return {"Obj1":listOfQuest.fetch()[0], "Obj2":listOfQuest.fetch()[1], "Obj3":listOfQuest.fetch()[2]}; //Unfortunately, this doesn't seem to work as expected as the returned object cannot be read in the template
}
});
myapp.html:
<template name="questions">
<h4> Tell us a little about yourself: </h4>
<form class="js-add-answers" id="add-answers">
{{#each getGroupQuestions}}
<label for="{{qNumber}}">{{qDescription}}</label>
<input type="text" class="form-control" id="{{qNumber}}" placeholder="{{qHints}}"/>
<p></p>
{{/each}}
<button class="btn btn-warning js-join-event">Save and Join</button>
<button class="btn btn-warning js-load-more">Save and load more Q</button>
</form>
</template>