2015-01-16 3 views
14

के अंदर अपरिभाषित लौटाता है Meteor.publish() फ़ंक्शंस में से एक में, this.userId का मान undefined है। मैं Meteor.userId() पर कॉल नहीं कर सकता क्योंकि यह not available inside a publish function है। आपको अब userId कैसे प्राप्त करना चाहिए?this.userId Meteor.publish

उत्तर

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, super, या new.target को बाध्य नहीं करता है।

तो यह निश्चित रूप से नहीं (2) है, क्या होता है जब आप Meteor.userId() लोग इन तुरंत इससे पहले कि आप ग्राहक पर विधि फोन करना?

+0

हाँ , यह 2 था। मैंने 'var = this.userId' को बस 'Meteor.publish' से ऊपर सेट किया था, इसलिए इसे सर्वर से बुलाया जा रहा था। 'Meteor.publish' के अंदर चलने से इसे ठीक किया गया। धन्यवाद! –

+14

इसके अलावा, पूर्णता के लिए, सुनिश्चित करें कि आप तीर फ़ंक्शन का उपयोग नहीं कर रहे हैं, यानी 'Meteor.publish (' चालान ', फ़ंक्शन() {वापसी invoices.find ({by: this.userId});}); ' ठीक से काम करेगा, जबकि 'Meteor.publish (' चालान ',() => {वापसी invoices.find ({by: this.userId});});' खाली कर्सर वापस कर देगा, क्योंकि इसमें कोई उपयोगकर्ता आईडी नहीं होगा। चूंकि एक तीर फ़ंक्शन "अपने स्वयं के, तर्क, सुपर, या new.target को बाध्य नहीं करता है"। –

+6

@ElijahSaounkine धन्यवाद! ईएस 6 द्वारा बिट। – joshperry

-4

आपको इसके बजाय Meteor.userId() का उपयोग करना चाहिए।

+0

यह कहता है "त्रुटि: Meteor.userId केवल विधि कॉल में ही बुलाया जा सकता है। प्रकाशित कार्यों में this.userId का उपयोग करें।" –

+0

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

+0

this.userId अनिर्धारित है –

-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; 
}); 
संबंधित मुद्दे