Search code examples
javascriptmeteormeteor-helper

Explain usage of {{#with}} in meteor JS


I was trying to retrieve the query result of this meteor helper using {{#with}} .. {{/with}}, but the template is not getting the data of the returned result.

So can someone explain what is the proper way of using the {{#with}} spacebar on meteor js. I tried using the {{#each}} ... {{/each}} and it perfectly gets the data.

Template.projectDetail.helpers({

    detail: function(){
        //var project = Session.get("aProject");
        if(Session.get("projectSelected")){
            var project = Project.find({_id: Session.get("projectSelected")}).fetch();
        }

        return project;
    }

});

<template name="projectDetail">
<div class="project">
    {{#with detail}}
    <h4 class="project-title">
        <span>{{name}}</span>
        <i class="glyphicon glyphicon-trash pull-right del"></i>
        <i class="glyphicon glyphicon-plus pull-right add"></i>
    </h4>
    <div class="clearfix"></div>

    <div class="project-description">
        <label>Project description:</label>
        <p>
        {{remarks}}
        </p>
    </div>
    {{/with}}
</template>

Solution

  • The problem is that fetch returns an array with all the documents matching the selector. You must pick the first (and only) document from this array by writing fetch()[0] instead of fetch() (or use findOne instead of find and fetch.