简体   繁体   English

#each for Meteor.js模板

[英]#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. 我已经进行了设置,以便当用户回答问题时,它会使用questionsAnswered更新他们的帐户,并更新问题usersTrueusersFalse数组。 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. 我试图向.find({active: true})添加一些额外的参数,但这不起作用。

I have a helper for my each loop. 每个循环都有一个帮手。 I have tried adding a if else that doesn't work. 我尝试添加一个if否则不起作用。

I would prefer to loop through the user's questionsAnswered array to see if they have already answered the question. 我希望遍历用户的questionsAnswered数组,看看他们是否已经回答了问题。

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: Javascript(帮助程序)代码:

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 1.创建一个服务器端文件,例如server / publications.js,以保存发布声明

2. Define it inside that file: 2.在该文件中定义它:

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

I ended up getting some help from someone at crater.io. 我最终从crater.io的某个人那里获得了一些帮助。 I was trying to add information to the javascript side to check to see if userId was not in the array. 我试图将信息添加到javascript端,以检查userId是否不在数组中。 I was introduced to the $nin operator so my code ended up looking like this. 我被介绍给$ nin运算符,因此我的代码最终看起来像这样。

Thanks for your responses! 感谢您的回复!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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