How to get userId in observe callback


#1

Hi,
I want to use observe for one of my collection on the server but I need to get userId,
I’m trying to use this.userId and also Meteor.userId() but not working! see the below code for more detail and error message
how to resolve it ?

Messages.find({state:"outbox"}).observe({
    added:  (doc) => {
    console.log(" observe ");
        console.log("userId : " + this.userId);  // undefined
        console.log("Meteor.userId(): " + Meteor.userId()); //  "Exception in queued task: Error: Meteor.userId can only be invoked in method calls. Use this.userId in publish functions."
        //.......
   }
});

thanks for you attention :slight_smile:


#2

@saeedjalalisj this is not possible because the callback only gets run once and not for every user that is connected to the server.