this.userId在Meteor.publish中返回undefined

Ale*_*yth 15 javascript publish userid meteor

在我的一个Meteor.publish()功能中,this.userId具有的价值undefined.我不能打电话,Meteor.userId()因为它在发布功能中不可用.你userId现在应该怎么样?

ric*_*ilv 69

有四种可能性:

  1. 没有用户登录.

  2. 您正在从服务器调用该方法,因此将没有与该调用相关联的用户(除非您从另一个将用户绑定到其环境的函数调用它,如另一个方法或订阅函数).

  3. 您甚至没有安装基于帐户的软件包(或任何加载项).我只是为了完整性而包含这个.

  4. 您正在使用ES6中的箭头功能. Meteor.publish('invoices', function() { return invoices.find({by: this.userId}); });将工作得很好,同时Meteor.publish('invoices', () => { return invoices.find({by: this.userId}); });将返回一个空光标,因为this将没有userId属性.这是因为箭头功能不绑定自己的this,arguments,super,或new.target.

如果它绝对不是(2),那么当您Meteor.userId()在客户端上进行方法调用之前立即记录时会发生什么?

  • 另外,为了完整起见,请确保您没有使用箭头功能,即`Meteor.publish('invoices',function(){return invoices.find({by:this.userId});});`will工作得很好,而`Meteor.publish('invoices',()=> {return invoices.find({by:this.userId});});`将返回空光标,因为这将没有userId.因为箭头函数"不绑定它自己的this,arguments,super或new.target". (16认同)
  • @ElijahSaounkine谢谢!位于ES6之下. (6认同)
  • 这是胖箭.谢谢! (4认同)