Tracker重新计算功能和子用户ID,匹配错误的例外情况

时间:2017-04-09 04:12:16

标签: javascript node.js meteor publish-subscribe

我有用户发布和订阅问题。当我点击用户名时,它应显示:[1]用户的个人资料图片; [2]该用户的所有帖子。

然而,它只显示加载微调器屏幕和以下错误。如果我尝试刷新或重新点击名称,页面最终会加载一些延迟。

我怀疑是客户竞争条件,但我尝试添加(!this._id) return [ ]this.ready();,帖子会在屏幕上闪烁一次,之后不会显示。我无法删除检查,因为它会抛出无检查错误。

如果在任何地方简化显示用户的帖子,请劝告。谢谢!

浏览器控制台上出现

错误:

  

Tracker重新计算功能的异常:meteor.js:930:11

     

undefined meteor.js:932:11

终端上显示的关键错误的选定部分:

> I20170409-11:51:58.787(8)? Exception from sub user id
> BRFKAiwEXKootpbwY Error: Match error: Expected string, got null
> (packages/check/match.js:34:1) I20170409-11:51:58.788(8)?     at
> Subscription.<anonymous> (server/server.js:86:2)
> I20170409-11:51:58.788(8)?     at Subscription.prepareOptions
> (packages/reywood_publish-composite/packages/reywood_publish-composite.js:440:1)
> I20170409-11:51:58.788(8)?     at Subscription._handler
> (packages/reywood_publish-composite/packages/reywood_publish-composite.js:408:1)
> I20170409-11:51:58.788(8)?     at packages/check/match.js:107:1 
> at Object.exports.Match._failIfArgumentsAreNotAllChecked
> (packages/check/match.js:106:1) I20170409-11:51:58.788(8)?     at
> maybeAuditArgumentChecks
> Match failed [400]

服务器发布

Meteor.publish('userDetails', function() {
    var fields = { 
        username    : 1, 
        emails      : 1, 
        profile     : 1,
        md5hash     : 1 
    };
    return Meteor.users.find( {}, { fields: fields });
});

Meteor.publishComposite('user', function (_id) {
    check(_id, String);  // cant remove this check, throws error
    //if (!this._id)  return this.ready(); or [] doesnt work 
        return {
            find: function() {
                return Meteor.users.find({ _id: _id }, { 
                    fields: { 
                        username    : 1, 
                        emails      : 1, 
                        profile     : 1,
                        md5hash     : 1 
                    }
                });
            },
                children: [
                    { 
                        find: function(user) { 
                            return Posts.find({ userId: user._id }); 
                        } 
                    } 
                ]
        };
});

路由器代码

Router.route('/users/:_id',      { 
  name: 'users.show',     
   onBeforeAction: function () {
      if( this.params._id === Meteor.userId() ){ 
         Router.go('profile');
      } else {
         this.next();
      }
   }
});

页面js

Template.usersShow.onCreated(function(){
   var self = this;
   self.autorun(function(){
      self.subscribe('user', Router.current().params._id);
   });
});
Template.usersShow.helpers({
   user: function () {
      return Meteor.users.findOne({ _id: Router.current().params._id });
   },
   posts: function () {
      return Posts.find({ userId: Router.current().params._id });
   }
});

1 个答案:

答案 0 :(得分:0)

我阅读了官方文档中的Pub / Sub部分,但从未在上下文_id中看到有关属性this的说明。

我认为将this._id更改为_id将正常运行。

Meteor.publishComposite('user', function (_id) {
  // Change `this._id` to `_id`
  if (!_id) return this.ready();  // or [];
  return {
    find: function() {
      return Meteor.users.find({
        _id: _id
      }, {
        fields: {
          username: 1,
          emails: 1,
          profile: 1,
          md5hash: 1
        }
      });
    },
    children: [
      {
        find: function(user) {
          return Posts.find({userId: user._id});
        }
      }
    ]
  };
})