swayziak
swayziak

Reputation: 353

Issue when publishing a post with Iron Router

I want to create a route where I'm able to reply to comments (.../comments/:_id/reply), but I'm having problems in publishing the post related with the comment.

Here is the code:

Publications

Meteor.publish('commentUser', function(commentId) {
    var comment = Comments.findOne(commentId);
    return Meteor.users.find({_id: comment && comment.userId});
});

Meteor.publish('commentPost', function(commentId) {
    var comment = Comments.findOne(commentId);
    return Posts.find({_id: comment && comment.postId});
});

Meteor.publish('singleComment', function(commentId) {
    return Comments.find(commentId);
});

Route

this.route('comment_reply', {
    path: '/comments/:_id/reply',
    waitOn: function() {
        return [
            Meteor.subscribe('singleComment', this.params._id),
            Meteor.subscribe('commentUser', this.params._id),
            Meteor.subscribe('commentPost', this.params._id)
        ]
    },
    data: function() {
            return {
                comment: Comments.findOne(this.params._id)
            }
    }

 });

Comment Reply Template

<template name="comment_reply">
    <div class="small-12 columns">
         {{# with post}}
              {{> postItem}}
         {{/with}}
    </div>

    <div class="small-12 columns">
          {{#with comment}}
          {{> comment}}
      {{/with}}
     </div>

     {{> commentReplySubmit}}   

</template> 

Comment Reply Helper

Template.comment_reply.helpers({
     postItem: function() {
         return Posts.findOne(this.comment.postId);
     }
});

When I access that route, the {{#with comment}} renders properly but the {{#with post}} doesn't appears. And if I try to render only {{> postItem}} without {{#with post}} it renders the html, but without the data.

The console prints this alert: You called Route.prototype.resolve with a missing parameter. "_id" not found in params

Thanks in advance!

Upvotes: 0

Views: 304

Answers (2)

Tobias
Tobias

Reputation: 4074

I think you mixed up the names of your template post (code is not given though) and your template helper postItem.

      {{#with post}}
          {{> postItem}}
      {{/with}}

Should probably be

      {{#with postItem}}
          {{> post}}
      {{/with}} 

Or do you have a template and a template helper called postItem?

Also there is a space between # and with, I am not sure if that is allowed.

Alternatively

Template.comment_reply.helpers({
     postItem: function() {
         return Posts.findOne(this.comment.postId);
     }
});

should be

Template.comment_reply.helpers({
     post: function() {
         return Posts.findOne(this.comment.postId);
     }
});

Upvotes: 1

sokeefe
sokeefe

Reputation: 637

What happens when you try to break apart the template into smaller templates? If I'm not mistaken, I don't think you can have multiple data context unless it has the same _Id. In this case, the post and the comment _Id will be different and would throw and error like the one you're getting. Try something like this:

<template name="comment_reply">
    <div class="small-12 columns">
         {{# with post}}
              {{> postItem}}
         {{/with}}
    </div>
</template>

<template name="postItem">
    <div class="small-12 columns">
      {{#with comment}}
          {{> comment}}
      {{/with}}
     </div>
</template> 

<template name="comment">
     {{> commentReplySubmit}}   
</template> 

You'll likely have to play around with the syntax of the templates and the routing.

Hope this helps!

Upvotes: 1

Related Questions