In one of my Meteor.publish()
functions, this.userId
has the value of undefined
. I can't call Meteor.userId()
because it's not available inside a publish function . How are you supposed to get userId
now?
There are four possibilities:
There is no user logged in.
You're calling the method from the server, and there will thus be no user associated with the call ( unless you're calling it from another function which will have a user bound to its environment, like another method or a subscribe function).
You don't even have the accounts-base package (or any of the add-ons) installed. I'm only including this for completeness.
You are using an arrow function in ES6. Meteor.publish('invoices', function() { return invoices.find({by: this.userId}); });
will work just fine, whilst Meteor.publish('invoices', () => { return invoices.find({by: this.userId}); });
will return an empty cursor, because this
will have no userId
property. This happens because an arrow function does not bind its own this
, arguments
, super
, or new.target
.
If it's definitely not (2), what happens when you log Meteor.userId()
immediately before you make the method call on the client?
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.userId()。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.