Meteor JS - Exception from sub id (id that does not exist)

2.1k views Asked by At

I20150615-07:11:17.859(9)? Exception from sub draftsList id GghnkQkdjNSTyHuQs Error: Match error: Expected object, got undefined I20150615-07:11:17.859(9)? at checkSubtree (packages/check/match.js:275:1) I20150615-07:11:17.859(9)? at check (packages/check/match.js:32:1) I20150615-07:11:17.859(9)? at [object Object].Meteor.publish.Meteor.users.find.userId [as _handler] (app/server/publications.js:44:3) I20150615-07:11:17.859(9)? at maybeAuditArgumentChecks (packages/ddp/livedata_server.js:1617:1) I20150615-07:11:17.859(9)? at [object Object]._.extend._runHandler (packages/ddp/livedata_server.js:950:1) I20150615-07:11:17.859(9)?
at [object Object]._.extend._startSubscription (packages/ddp/livedata_server.js:769:1) I20150615-07:11:17.859(9)?
at [object Object]._.extend.protocol_handlers.sub (packages/ddp/livedata_server.js:582:1) I20150615-07:11:17.859(9)?
at packages/ddp/livedata_server.js:546:1 I20150615-07:11:17.860(9)? Sanitized and reported to the client as: Match failed [400]

I am working on a meteor project and I'm getting this weird error on my terminal.

The problem is when it says Exception from sub draftsList id GghnkQkdjNSTyHuQs Error: Match error: Expected object, got undefined, the id always changes on page refresh, and are not in my database (nowhere to be seen).

The thing is, everything works find..

Here is my publication-subscription

Publication

Meteor.publish('draftsList', function (options) {   
  check(this.userId, String);
  check(options, {
    limit: Number
  });

  var drafts = Drafts.find({'user._id': this.userId}, options);

  return drafts;
});

Subscription

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

There are 2 answers

0
David Weldon On BEST ANSWER

You are subscribing to draftsList twice in your route. The first subscription is without any parameters, which will make options be undefined instead of an object. Just remove the first subscription and the problem should go away.

Also note that using check for this.userId may work in this case but could cause problems if you were using waitOn in your route. The generally accepted pattern is to return [] or this.ready() when this.userId is undefined in a publisher which requires an authenticated client.

0
saimeunt On

Try to remove your check against this.userId in your publication, you don't need to check this property validity since Meteor already does it for you.

Meteor.publish('draftsList', function (options) {   
  check(options, {
    limit: Number
  });
  var drafts = Drafts.find(this.userId, options);
  return drafts;
});