Meteor userId present but user undefined

During the rendering of my responsive component, I get Meteor.user() null. Then I tried to access Meteor.userId()and got it correctly as a registered user id. Also tried to access the user using Meteor.users.findOne(), but failed.

My question is: why is the user object undefined, although the user id is available?

I used the following code snippet for testing:

var uid = Meteor.userId();
console.log(uid);                                 // printed the _id correctly

var usr = Meteor.user();                
console.log(usr);                                 // undefined 

var usr1 = Meteor.users.findOne({_id: uid});
console.log(usr1);                                // undefined 
+4
source share
2 answers

Meteor.userId() . Meteor.user() , DDP , .

profile. , .

:

:

Meteor.publish('me',function(){
  if ( this.userId ) return Meteor.users.find(this.userId,{ fields: { key1: 1, key2: 1 ...}});
  this.ready();
});

:

Meteor.subscribe('me');

, , , . , .

+3

Meteor.user() , :

Tracker.autorun(function(){
  var uid = Meteor.userId();
  console.log(uid);                                 // printed the _id       correctly

  var usr = Meteor.user();                
  console.log(usr);                                 // undefined 

  var usr1 = Meteor.users.findOne({_id: uid});
  console.log(usr1);  
});

undefined .

+2

Source: https://habr.com/ru/post/1660084/


All Articles