Blaze Helper not working with {{#each}}

Hey guys,

I have a very simple situation here, but Blaze is managing to drive me crazy with its cryptic documentation and confusing syntax.

I have a survey collection, with an array of questions inside. This array contains only strings that point to the questions’ ID’s.

I want to create a simple page where all surveys and questions inside of them are shown.

Therefore I created two templates on the same html file: one to loop through the surveys and the second to loop through the questions. My goal is that for each survey found, the page prints each question found. Tried a million different things and it doesn’t work. Here is my code:

answer.html

<template name="Answer">
    <div class="container">
        <div class="row">
            <div class="col s12">
                {{#each survey in surveys}}
                    <h4>{{survey.name}}</h4>
                    {{> Question (questions survey)}}
                {{/each}}
            </div>
        </div>
    </div>
</template>

<template name="Question">
    <div class="row">
        <div class="col s12">
            {{#each question in questions}}
                <h6>{{question.text}}</h6>
            {{/each}}
        </div>
    </div>
</template>

answer.js

if(Meteor.isClient){

    let companyId = Meteor.userId(); // Grabs current logged-in company's ID

    Meteor.subscribe('surveys',companyId); // Subscribes to current company's surveys

    Template.Answer.helpers({

        surveys(){
            return Surveys.find({companyId: companyId}); // Returns current company's surveys
        }

    });

    Template.Question.helpers({

        questions(survey){
            console.log(survey);
            const questions = survey.questions; // Grabs questions array from the passed survey object - REMEMBER: array contains only question id's
            for(let i in questions){
                const question = Questions.find({_id: questions[i]},{fields: {questionText:1}}); // Returns question text for each question ID passed
                return question;
            }

        }

    })

}

Result is this error: Exception in queued task Error: No such function: questions

Any idea what I could do? Thanks!

A couple of things - you pass (questions survey) from the Answer template as the data context to the Question template so:

  1. you need to define the questions helper on the Answer template, not theQuestion` template
  2. in the Question template your each should be {{#each question in this}}

alternatively, you could pass the survey through to the Question template, and use {{#each question in (questions this)}}

1 Like