Search code examples
javascriptmeteormeteor-blazespacebarsmeteor-helper

#each for Meteor.js template


I am trying to make it so users only see questions they haven't answered already. I have set it up so when user answers a question it updates their account with questionsAnswered and updates the questions usersTrue and usersFalse arrays. I am using the each template function to loop through my question collection to show all active questions.

I have tried to add some add extra parameter to the .find({active: true}) but that doesn't work.

I have a helper for my each loop. I have tried adding a if else that doesn't work.

I would prefer to loop through the user's questionsAnswered array to see if they have already answered the question.

Template

<template name="questionCard">
{{#each questions}}
        <div id="{{_id}}" class="card">
            {{ que}}
        </div>
        <div>
            <a class="no option" href="#">No</a>
            <a class="yes option" href="#">Yes</a>
        </div>
{{/each}}
</template>

Javascript (helper) code:

Template.questionCard.helpers({
'questions': function(){
    var currentUser = Meteor.userId();
    return QuestionList.find({active: true});
}
});

I would like for the card to disappear after they answer, but that problem should solve itself if the loop is fixed.

Any help would be greatly appreciated.


Solution

  • I ended up getting some help from someone at crater.io. I was trying to add information to the javascript side to check to see if userId was not in the array. I was introduced to the $nin operator so my code ended up looking like this.

    Thanks for your responses!

    Template.questionCard.helpers({
    'questions': function(){
        var currentUser = Meteor.userId();
        return QuestionList.find({active: true, usersTrue: {$nin: [currentUser]}, 
        usersFalse: {$nin: [currentUser]}});
    }
    });