简体   繁体   中英

Meteor Iron Router, Pub Sub causing weird behavior

I am trying to make sing post page a route where it does a several things using iron:router

  1. Uses the template postPage
  2. Subscribes to publication of singlePost , userStatus (shows status and info of Author of single post page'), comments .
  3. Grabs Comments documents that has field of postId : this.params._id
  4. Increments Comments List by Session.get('commentLimit')

Here is the code I currently have.


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

Everything works fine, but I found one thing to be inconsistent. Here is the problem I am having...

  1. User goes into single post page and adds comment (everything works fine).
  2. User goes into a different single post page and adds comment (everything works fine).
  3. Here is where the problem begins
    • The user at any time, goes into another route that is not the single post page.
  4. User goes back into single post page
    • The comments are not showing.
    • New comments will be added into DB but still wont show
  5. This problem only goes away when meteor reset or manual deletion of all comments in MongoDB is performed.

Is there a better way that I can code my routing and related code to stop this weird behavior from happening?

Or even if there is a better practice.

Your publish is publishing comments without any postId filter.

Your helper, filters by postId . Maybe the 4 comments that get published are the ones that do not belong to the current post that is open?

Could you try updating, your subscription to

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

and your publication to

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

so that only the same posts' comments are published?

I have figured it out. I have updated the following codes.

So far it is not showing weird behavior...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM