简体   繁体   中英

#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.

Make sure you do the following:

1. Create a server side file, such as server/publications.js, to hold the publish declaration

2. Define it inside that file:

Meteor.publish("questionList", function() {
  return QuestionList.find({});
});

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]}});
}
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM