this.userId在Meteor.publish中返回undefined

时间:2015-01-16 19:12:33

标签: javascript meteor publish userid

在我的Meteor.publish()个功能之一中,this.userId的值为undefined。我无法致电Meteor.userId(),因为它是not available inside a publish function。你现在应该如何获得userId

3 个答案:

答案 0 :(得分:69)

有四种可能性:

  1. 没有用户登录。

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

  3. 您甚至没有安装accounts-base个包(或任何加载项)。我只是为了完整性而将其包括在内。

  4. 您正在使用ES6中的箭头功能。 Meteor.publish('invoices', function() { return invoices.find({by: this.userId}); });工作正常,而Meteor.publish('invoices', () => { return invoices.find({by: this.userId}); });将返回空光标,因为this将没有userId属性。 发生这种情况是因为箭头函数不绑定其自己的thisargumentssupernew.target

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

答案 1 :(得分:-1)

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

答案 2 :(得分:-4)

您应该使用Meteor.userId()。

相关问题