Meteor passing value into helper function(Blaze and meteor)

I am using a helper inside another helper. I am trying to pass a value ‘post_id’, that i am getting dynamically from ‘show_post’ helper.I want to pass it and then use it inside the query that is returning a set of result back to helper. for some reason , app is crashing. Can someone guide me through this.

{{#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;
}
});

thanks in advance for those who is going to give any effort to this.

Have you tried

<template name="post">
 {{#each posts}}
   {{#each show_comment}}
    <span> <p>{{postedBy}}: <h5>{{commenttext}}</h5>{{createdAt}}</p></span>                      
   {{/each}}
 {{/each}}
</template>

where

Template.post.onCreated(function(){
	var instance = this;
	instance.posts = {
		'one':{
			comments: [
				{postedBy: 'xxx', commenttext: 'xxx', createdAt: 'xxx'},
				{postedBy: 'yyy', commenttext: 'yyy', createdAt: 'yyy'},
				{postedBy: 'zzz', commenttext: 'zzz', createdAt: 'zzz'},
			]
		},
		'two': {
			comments: [
				{postedBy: 'aaa', commenttext: 'aaa', createdAt: 'aaa'},
				{postedBy: 'bbb', commenttext: 'bbb', createdAt: 'bbb'},
				{postedBy: 'ccc', commenttext: 'ccc', createdAt: 'ccc'},
			],
		}
	}
});
Template.post.helpers({
	posts(){
		var posts = Template.instance().posts;
		return Object.keys(posts).map(function(p){ return {post_id: p}});
	},
	show_comments() {
                // 'this' is the context from the outer #each
		var posts = Template.instance().posts;
		return posts[this.post_id].comments;
	},
});

Let me address the issue again. The first helper generates a array(Mongo Db result).I want to use this array’s elements(that are dynamic) in next helper. So i want to use a variable that can hold the value of array element of first helper and then pass it to next helper. In second helper, i want to pass this variable and use this variable to sort a result in Mongo query. I am new to this so i am unable to understand how this instance concept works.

You don’t really need a variable to hold the result, you can just pass a Mongo collection cursor to #each, like this (assuming your posts are in PostsMongoCollection and your comments in CommentsMongoCollection.

Template.post.helpers({
	posts(){
		return PostsMongoCollection.find({},{sort:{postedBy:1}});
	},
	show_comments() {
		return CommentsMongoCollection.find({post_id: this.post_id});
	},
});
1 Like

thanks a lot . this worked for me. i am great-full for your response.

To be complete, it is possible to do what you wanted. The syntax is here.

In your case use:

{{#each (show_comment post_id)) }} ...

Thanks for your response.This syntax is also working.