【问题标题】:Comments appear in every post - Meteor评论出现在每个帖子中 - Meteor
【发布时间】:2015-10-11 17:20:55
【问题描述】:

已经两周了,我仍然无法让 cmets 出现在他们写的帖子中。每个评论都会出现在每个帖子中。在尝试了很多不同的教程、信息或视频(基本上是谷歌和流星文档给我的所有内容)后,我几乎精神崩溃了,但我一直失败得很惨......

服务器:

Meteor.publish("posts", function () {
    return Posts.find();
});

Meteor.publish("comments", function() {
  return Comments.find();
});

Single-post.js

Template.singlePost.helpers({
    comments: function () {
      return Comments.find({},{sort: {createdAt: -1}});
    }     
});

Template.singlePost.events({
    "submit .new-comment": function (event) {
        var text = event.target.text.value;
        Meteor.call("addComment", text);
        event.target.text.value = "";
        return false;
    }
});

Meteor.methods 内部:

addComment: function (text) {
    if (! Meteor.userId()) {
        throw new Meteor.Error("not-authorized");
    }

Comments.insert({
  text: text,
  createdAt: new Date(),
  owner: Meteor.userId(),
  username: Meteor.user().username
});
},

最后是路由器:

Router.map(function(){
    this.route('top', {path:'/top'});
    this.route('trending', {path:'/trending'});
    this.route('new', {path:'/new'});
    this.route('singlePost', {path:'/post/:_id',
        data:function(){
            return Posts.findOne({_id:this.params._id})
        }
                             });
    })

我知道我没有在路由器中包含评论或缺少有关 cmets 的其他一些内容,因为我尝试了无数不同的事情但都失败了,所以我想为我未来的助手保持干净......

提前致谢!

【问题讨论】:

    标签: meteor


    【解决方案1】:

    所有的 cmets 都因此被显示:

    Template.singlePost.helpers({
      comments: function () {
        return Comments.find({},{sort: {createdAt: -1}});
      }
    });
    

    您没有通过 find() 调用任何参数来为某个帖子调出 cmets。例如:

    Template.singlePost.helpers({
      comments: function () {
        return Comments.find({postId: ...},{sort: {createdAt: -1}});
      }
    });
    

    我需要查看您的完整代码才能提供比这更有帮助的内容。

    【讨论】:

      【解决方案2】:

      您的 cmets 需要通过 id 加入您的帖子。所以addComment 应该是这样的:

      addComment: function (postId, text) {
        check(postId, String);
        check(text, String);
      
        if (!this.userId) {
          throw new Meteor.Error(403, 'not-authorized');
        }
      
        Comments.insert({
          text: text,
          createdAt: new Date(),
          owner: this.userId,
          username: Meteor.user().username,
          postId: postId
        });
      }
      

      现在,您所有的 cmets 都将通过 postId 与帖子相关联。然后在你的 comments 助手中,你可以像这样加入这两个:

      comments: function () {
        selector = {postId: this._id};
        options = {sort: {createdAt: -1}};
        return Comments.find(selector, options);
      }
      

      最后,您的提交事件:

      submit: function (event) {
        event.preventDefault();
        var text = event.target.text.value;
        Meteor.call('addComment', this._id, text);
      }
      

      以上所有内容都假设当前上下文是post 文档,如路由器中的data 挂钩所示。

      【讨论】:

      • 老兄...我他妈的爱你!你不仅解决了我的问题,还教会了我新的东西,我不知道,我也理解了我以前的错误。太棒了!
      • 是的,尽管这个答案很有效,但这很好 - 但是订阅呢。 OP 的代码订阅了所有数据,这将在加载时终止您的网站。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-09-20
      • 1970-01-01
      • 2013-04-12
      • 1970-01-01
      • 1970-01-01
      • 2013-11-03
      • 2021-07-13
      相关资源
      最近更新 更多