在辅助函数中传递一个变量

passing a variable inside helper funtion

我在另一个助手中使用了一个助手。我正在尝试传递一个值“post_id”,我从“show_post”动态获取 helper.I 想传递它然后在返回一组的查询中使用它结果返回给助手。由于某种原因,应用程序崩溃了。谁能指导我解决这个问题。

{{#each show_post}}
{{posttext}}

{{postedBy}}

    {{#each show_comment({{post_id}}) }}  
    //<--- i am passing the value to helper like this.
    <span> <p>{{postedBy}}: <h5>{{commenttext}}</h5>{{createdAt}}</p></span>                      
    {{/each}}

    {{/each}}
    Template.display_block.helpers({
    show_post(){
    return PostComment.find({parent: 0},{sort: {createdAt: -1}});
    }
    });

Template.display_block.helpers({
show_comment(e){
var t1 = e;
var now = PostComment.find({post_id:{$regex:new RegExp(’^’ + t1)}});
return now;
}
});

第一个助手生成一个数组(Mongo Db 结果)。我想在下一个助手中使用这个数组的元素(动态的)。所以我想使用一个变量来保存第一个助手的数组元素的值,然后将它传递给下一个助手。在第二个助手中,我想传递这个变量并使用这个变量对 Mongo 查询中的结果进行排序。我是新手,所以我无法理解这个实例

首先,您不需要将辅助参数包裹在双花括号或括号中。

{{#each show_comment post_id}}

会做你需要的。

你也让自己的生活变得比必要的更复杂。您可以在代码中通过 this 使用当前数据上下文。也不清楚你为什么使用正则表达式,除非你将某些东西连接到 post _id.

这使您可以简化为:

html:

{{#each show_post}}
  {{posttext}}
  {{postedBy}}
  {{#each show_comment}}
    <span><p>{{postedBy}}: <h5>{{commenttext}}</h5>{{createdAt}}</p></span>                      
  {{/each}}
{{/each}}

js:

Template.display_block.helpers({
  show_comment(){
    return PostComment.find({post_id: this._id);
  }
});