简体   繁体   中英

Displaying an array Meteor Helper

For each question I'm trying to display the choices array in an li using Meteor Helpers.

MongoDB my collection is:

{ "_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" ] }

code for the helper is

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

});

code for the html is

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

For choices it is returning

1. Blue, Green, Red, Black

I want it to return

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

I tried

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

got error message

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

still error

Any idea how to get the array to return as a li item?

Thank you

I used a meteor methods to perform the query where you have return GenKnow.find ({}); I would query using a

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
          }

        } 
    });
},

Then there in my method

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

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