2015-01-16 24 views

回答

63

有四種可能性:

  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屬性。 發生這種情況是因爲箭頭功能不綁定它自己的this,arguments,supernew.target

如果它絕對不是(2),什麼,當你登錄Meteor.userId()你讓客戶端上的方法調用之前發生了什麼?

+0

Yeah ,它是2.我在'Meteor.publish'上面設置了'var = this.userId',所以它從服務器被調用。把它移到Meteor.publish裏面。謝謝! –

+14

此外,爲了完整性,請確保您沒有使用箭頭函數,即'Meteor.publish('invoices',function(){return invoices.find({by:this.userId});});' ('invoices',()=> {return invoices.find({by:this.userId});});'將返回空遊標,因爲它沒有userId。因爲一個箭頭函數「不綁定它自己的this,arguments,super或new.target」。 –

+6

@ElijahSaounkine謝謝!位由ES6。 – joshperry

-4

您應該使用Meteor.userId()來代替。

+0

它說「錯誤:Meteor.userId只能在方法調用中調用,在發佈函數中使用this.userId」。 –

+0

Meteor.publish(「my_channel」,function(){ var userId = this.userId; myFunction(userId); }); –

+0

this.userId is undefined –

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