在发布函数中使用this.userId

时间:2017-05-13 02:25:57

标签: meteor

此Meteor服务器代码给出以下错误:

Meteor.publish('myCol', function (age) {
  if (!this.userId) return;
  if (Meteor.user().profile.hasOwnProperty('gotNoAge')) return; //<=== error line
  console.log('publishing for age: ' + age);

    return MyCol.find({age: age}, {
      fields: {
        myField: true
      }, limit: 1
    });

});
  

错误:Meteor.userId只能在方法调用中调用。在发布函数中使用this.userId。

我尝试更改&#34;错误行&#34;以下无济于事。 if (Meteor.users.find({_id:this.userId}).profile.hasOwnProperty('gotNoAge')) return; 有什么建议吗? THX

1 个答案:

答案 0 :(得分:0)

尝试以下代码

Meteor.publish('myCol', function (age) {
  if (!this.userId) return;
  var user = Meteor.users.findOne({_id: this.userId});
  if (user.profile.hasOwnProperty('gotNoAge')) return; 
  console.log('publishing for age: ' + age);

  return MyCol.find({age: age}, {
    fields: {
      myField: true
    }, limit: 1
  });
});
相关问题