Meteor Iron Router,Pub Sub 导致奇怪的行为

Meteor Iron Router, Pub Sub causing weird behavior

我正在尝试让 sing post 页面成为一条路线,它使用 iron:router

做几件事
  1. 使用模板postPage
  2. 订阅 singlePostuserStatus 的出版物(显示单个 post 页面的作者的状态和信息'),comments
  3. 抓取 Comments 个字段为 postId : this.params._id
  4. 的文档
  5. 增加评论列表 Session.get('commentLimit')

这是我目前拥有的代码。


Router.js

Router.route('/posts/:_id', {
  name: 'postPage',
  subscriptions: function() {
    return [
      Meteor.subscribe('singlePost', this.params._id),
      Meteor.subscribe('userStatus'),
      Meteor.subscribe('comments', {
        limit: Number(Session.get('commentLimit'))
      })
    ];
  },
  data: function() {
    return Posts.findOne({_id:this.params._id});
   },
});

Publications.js

Meteor.publish('singlePost', function(id) {
  check(id, String);
  return Posts.find(id);
});

Meteor.publish('comments', function(options) {
  check(options, {
    limit: Number
  });
  return Comments.find({}, options);
});

Template.postPage.onCreated

Template.onCreated( function () {
  Session.set('commentLimit', 4);
});

Template.postPage.helpers

Template.postPage.helpers({
    comments: function () {
      var commentCursor = Number(Session.get('commentLimit'));
      return Comments.find({postId: this._id}, {limit: commentCursor});
    },
});

Template.postPage.events

Template.postPage.events({
    'click a.load-more-comments': function (event) {
      event.preventDefault();
      Session.set('commentLimit', Number(Session.get('commentLimit')) + 4)
    }
});

一切正常,但我发现有一件事不一致。 这是我遇到的问题...

  1. 用户进入单个 post 页面并添加评论(一切正常)。
  2. 用户进入另一个 post 页面并添加评论(一切正常)。
  3. 这是问题开始的地方
    • 用户在任何时候进入另一个不是单一 post 页面的路径。
  4. 用户返回单个 post 页面
    • 评论没有显示。
    • 新评论将添加到数据库中,但仍然不会显示
  5. 此问题只有在 meteor reset 或手动删除 MongoDB 中的所有评论后才会消失。

有没有更好的方法来编写我的路由和相关代码来阻止这种奇怪的行为发生?

或者即使有更好的做法。

您发布的评论没有任何 postId 过滤器。

您的帮手,按 postId 筛选。也许发布的 4 条评论不属于当前打开的 post?

你能尝试更新你对

的订阅吗
Meteor.subscribe('comments', {
    postId: this.params._id
}, {
    limit: Number(Session.get('commentLimit'))
})

以及您的出版物

Meteor.publish('comments', function(filter, options) {
    check(filter, {
        postId: String
    });
    check(options, {
        limit: Number
    });
    return Comments.find(filter, options);
});

以便只发布相同 post 的评论?

我想通了。我更新了以下代码。

到目前为止它没有表现出奇怪的行为...

Publications.js

Meteor.publish('comments', function(postId, limit) {
  check(postId, String);
  check(limit, Number);
  return Comments.find({postId:postId}, {limit:limit});
});

Router.js

Router.route('/posts/:_id', {
  name: 'postPage',
  subscriptions: function () {
    return [
      Meteor.subscribe('singlePost', this.params._id),
      Meteor.subscribe('userStatus'),
      Meteor.subscribe('comments', this.params._id, Number(Session.get('commentLimit')))
    ];
  },
  data: function() {
    return Posts.findOne({_id:this.params._id});
   },
});