Search code examples
javascriptarrayscollectionsmeteoriron-router

Meteor.js Routing with an Array


I have a collection of questions, each question with an id:

[
  {
    id: 0,
    question: "What's up?"
},

.....

I also have a collection of question lists/arrays. Each item in the list/array is a questions id.

I have a template to link to the questions in the list:

<template name="questionListIDContent">
    {{#each question}}
        <a href="{{pathFor 'myQuestion'}}" class="discuss btn btn-default btn-sm">Answer</a>
    {{/each}}
</template>

The route is:

    Router.route('/myquestion/:_id', {
  name: 'myQuestion',
  data: function() {
    return Questions.findOne(this.params._id);
    }
});

The helper is:

 Template.questionListIDContent.helpers({
  question: function() {
    var temp = QuestionsList.findOne({id: this.lessonID});
    var temp2 = temp.questionID // Array of question IDs
    return Questions.findOne({id: { $in: temp2 }});
  }
});

I am trying to use findOne with an array containing the list of questions to get required questions to use in the template. Anyone see what I am doing wrong here? Is there a better way.


Solution

  • Don't use findOne, use find. FindOne returns one item, so you can run over them in an each block. Find returns a cursor which you can run through with each.