【问题标题】:Displaying an array Meteor Helper显示一个数组 Meteor Helper
【发布时间】:2017-06-09 07:07:13
【问题描述】:

对于每个问题,我都尝试使用 Meteor Helpers 在 li 中显示选择数组。

MongoDB 我的收藏是:

{ "_id" : "AS7zMpdqWzpRyzdDw", "question" : "Favorite Color?", "answer" : "Blue", "choices" : [ "Blue", "Green", "Red", "Black" ] }
{ "_id" : "RaDxyRjDyL4at6oN4", "question" : "Favorite Truck?", "answer" : "Ram", "choices" : [ "Silverado", "Tundra", "Ram", "Titan" ] }
{ "_id" : "n6kvXfoLKueTZiR2A", "question" : "Favorite Animal?", "answer" : "Dog", "choices" : [ "Cat", "Dog", "Horse", "Fish" ] }

助手的代码是

Template.genKnow.helpers({
question(){
    return GenKnow.find({});
},

});

html 的代码是

    {{#each question}}
<div id="testQuestions">
    <div class="question" id="question">
        <h3 id="quesNum">QUESTION</h3>
        <p id="questions">{{question}}</p>
    </div>

    <div class="choices">
        <h3>CHOICES</h3>
        <ol id="choices">
            <li>{{choices}}</li>
        </ol>
    </div>

    <div class="answer">
        <h3>CORRECT ANSWER</h3>
        <p id="answer">{{answer}}</p>
    </div>
</div>
{{/each}}

screen shot of what it is returning

对于选择它正在返回

1. Blue, Green, Red, Black

我希望它返回

1. Blue
2. Green
3. Red
4. Black

我试过了

<div class="choices">
        <h3>CHOICES</h3>
        <ol id="choices">
            {{#each {{choices}} }}
                <li></li>
            {{/each}}
        </ol>
    </div>

收到错误消息

        <div class="choices">
        <h3>CHOICES</h3>
        <ol id="choices">
            {{#each question.choices }}
                <li></li>
            {{/each}}
        </ol>
    </div>

还是错误

知道如何让数组作为 li 项返回吗?

谢谢

【问题讨论】:

    标签: javascript meteor


    【解决方案1】:

    我使用了一个流星方法来执行你已经返回 GenKnow.find ({}); 的查询 我会使用

    查询
    Template.genKnow.helpers({
    question(){
        Meteor.call('meteorMethod', dataObject, function(error, success) { 
            if (error) { 
                console.log('error', error); 
            } 
            if (success) { 
              for (var i = 0; i < success.length; i++) {
                  var element = success[i];
                  return element
              }
    
            } 
        });
    },
    

    然后在我的方法中

    Meteor.methods({ 
        meteorMethod: function() { 
             Return GenKnow.find ({this.userId}) fetch ();
        } 
    });
    

    【讨论】:

    • 您不应该在帮助程序中调用方法,而应在事件处理程序或组件生命周期挂钩(或其他适合处理和改变状态的地方)中调用方法。
    • 感谢您的帮助。我不得不做一些调整,最后使用 forEach 来显示选择数组。它似乎正在工作。欣赏!
    猜你喜欢
    • 2014-03-13
    • 1970-01-01
    • 2015-11-02
    • 2015-03-17
    • 1970-01-01
    • 1970-01-01
    • 2016-12-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多