【问题标题】:Use this.userId in publish functions在发布函数中使用 this.userId
【发布时间】:2017-05-13 12:19:43
【问题描述】:

此 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。

我尝试将“错误行”更改为以下内容,但无济于事。 if (Meteor.users.find({_id:this.userId}).profile.hasOwnProperty('gotNoAge')) return; 有什么建议吗?谢谢

【问题讨论】:

  • 试试findOne

标签: meteor


【解决方案1】:

试试下面的代码

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

【讨论】:

    猜你喜欢
    • 2016-04-04
    • 1970-01-01
    • 1970-01-01
    • 2017-03-04
    • 1970-01-01
    • 2017-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多