0

我想创建一个可以回复评论的路线(.../comments/:_id/reply),但在发布与评论相关的帖子时遇到问题。

这是代码:

出版物

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

路线

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)
            }
    }

 });

评论回复模板

<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> 

评论回复助手

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

当我访问该路线时,{{#with comment}} 会正确呈现,但 {{#with post}} 不会出现。如果我尝试只渲染 {{> postItem}} 而没有 {{#with post}} 它会渲染 html,但没有数据。

控制台打印此警报:您调用 Route.prototype.resolve 时缺少参数。在参数中找不到“_id”

提前致谢!

4

2 回答 2

1

当您尝试将模板分解为更小的模板时会发生什么?如果我没记错的话,我认为你不能拥有多个数据上下文,除非它具有相同的 _Id。在这种情况下,帖子和评论 _Id 会有所不同,并且会像您得到的那样抛出错误。尝试这样的事情:

<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> 

您可能不得不使用模板和路由的语法。

希望这可以帮助!

于 2013-12-28T19:34:54.277 回答
1

我认为你混淆了你的模板名称post(虽然没有给出代码)和你的模板助手postItem

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

应该是

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

或者你有一个模板和一个名为的模板助手postItem

#和之间还有一个空格with,我不确定是否允许。

或者

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

应该

Template.comment_reply.helpers({
     post: function() {
         return Posts.findOne(this.comment.postId);
     }
});
于 2013-12-28T20:41:33.970 回答