Javascript this.userId返回Meteor.publish中未定义的内容

Javascript this.userId返回Meteor.publish中未定义的内容,javascript,meteor,publish,userid,Javascript,Meteor,Publish,Userid,在我的一个Meteor.publish()函数中,this.userId的值为undefined。我不能调用Meteor.userId(),因为它太小了。您现在应该如何获取userId?您应该使用Meteor.userId()。有四种可能: 没有用户登录 您正在从服务器调用该方法,因此将没有与该调用关联的用户(除非您正在从另一个函数调用它,该函数将有一个用户绑定到其环境,如另一个方法或订阅函数) 您甚至没有安装该软件包(或任何附加组件)。我只是为了完整起见才把它包括进去 您正在ES6中使用箭头函

在我的一个
Meteor.publish()
函数中,
this.userId
的值为
undefined
。我不能调用Meteor.userId(),因为它太小了。您现在应该如何获取
userId

您应该使用Meteor.userId()。

有四种可能:

  • 没有用户登录

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

  • 您甚至没有安装该软件包(或任何附加组件)。我只是为了完整起见才把它包括进去

  • 您正在ES6中使用箭头函数。
    Meteor.publish('invoices',function(){return invoices.find({by:this.userId});})可以正常工作,而
    Meteor.publish('invoices',()=>{returninvoices.find({by:this.userId});})
    将返回一个空光标,因为
    将没有
    用户ID
    属性。 这是因为箭头函数不绑定自己的
    This
    参数
    super
    new.target

  • 如果肯定不是(2),那么在对客户端进行方法调用之前立即登录Meteor.userId()会发生什么情况?

    修复:
    
    FIXED:
    
    import { Meteor } from 'meteor/meteor';
    import { Roles } from 'meteor/alanning:roles';
    import _ from 'lodash';
    
    import { check } from 'meteor/check';
    
    
    import Corporations from '../corporations';
    
    Meteor.publish('corporations.list', () => {
      const self = this.Meteor; // <-- see here 
      const userId = self.userId();
      const user = self.user();
    
      let filters = {};
    
      if (user) {
        if (!Roles.userIsInRole(userId, ['SuperAdminHolos'])) { // No Está en el Rol SuperAdminHolos
          filters = { adminsEmails: { $in: _.map(user.emails, 'address') } };
        }
        return Corporations.find(filters);
      } else return;
    });
    
    从“流星/流星”导入{Meteor}; 从“meteor/alanning:Roles”导入{Roles}; 从“lodash”进口; 从“meteor/check”导入{check}; 从“../corporates”导入公司; Meteor.publish('corporates.list',()=>{
    const self=this.Meteor;//它说“错误:Meteor.userId只能在方法调用中调用。在发布函数中使用this.userId。”Meteor.publish(“我的频道”,function(){var userId=this.userId;myFunction(userId);});this.userId未定义
    Meteor.publish()
    无权访问
    Meteor.userId()
    :“Anywhere but publish function”是的,它是2。我在Meteor.publish上面设置了
    var=this.userId
    ,所以它是从服务器调用的。将它移动到
    Meteor.publish
    中修复了它。谢谢!另外,为了完整性,请确保您没有使用箭头函数,即
    Meteor.publish('invoices',function(){return invoices.find({by:this.userId});})
    可以正常工作,而
    Meteor.publish('invoices',()=>{return invoices.find({by:this.userId});
    将返回空游标,因为它没有userId。因为箭头函数“不绑定自己的this、arguments、super或new.target”“@ElijahSaounkine谢谢你!被ES6咬了一口。那是胖箭。谢谢!干杯!这让我发疯了,对我来说是4。